'How do I re-gather device facts to remove any partitioned disks?

I'm building a role for which I'm trying to gather disks without any partitions using

disks: "{{ ansible_devices | dict2items | selectattr('key', 'match', '^sd.*')  |  selectattr('value.partitions', 'match', '{}' )  | list  }}"

and then mount on the partition. But, as I'm running over a loop I wanted to update the list/disks when the mount and partition is completed.

For example, if my list at the start looks like

disks: [sdb,sdc,sdd,sde]

when the partitioning and mounting on sdb using the mount module in Ansible is done, the list should be updated and look something like this

disks: [sdc,sdd,sde]

How can I achieve this?



Solution 1:[1]

You should be able to refresh the facts by calling the setup module after your mounts.

So, a plain and simple task reading:

- setup:

And Ansible will gather the facts of the host(s) again, so, your partition list should be up-to-date.


Since you cannot do it in the task itself, what you could do, in your role is to have an include_tasks that would make an unicity of the tasks: parted (or whatever you are using to partition); mount and setup.

Something like:

- include_tasks: partition-mount-and-setup.yml
  loop: "{{ mounts }}"

And in partition-mount-and-setup.yml, something like

- parted:
    device: "{{ item.mount_point }}"
    number: 1
    state: present
    part_end: "{{ item.size }}"

- mount:
    src: "{{ item.mount_point }}"
    path: "/mnt/{{ item.mount_point }}"
    state: mounted
    fstype: ext2

- setup:

All this is untested, but you could just follow the general logic

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1