I'm preparing a playbook to find the newly scanned HDD from vmware, I used below filtering to fetch the no. of HDDs:
before_add: "{{ hostvars[inventory_hostname].ansible_devices.keys() | select('string') | list }}"
OUTPUT- ['sr0', 'sda', 'sdb', 'sdc', 'dm-2', 'dm-3', 'dm-0', 'dm-1']
then add a new HDD using vmware_guest_disk module and then I executed the setup module to fetch the latest no. of disks
after_add: "{{ hostvars[inventory_hostname].ansible_devices.keys() | select('string') | list }}"
OUTPUT: ['sr0', 'sda', 'sdb', 'sdc', 'sdd', 'dm-2', 'dm-3', 'dm-0', 'dm-1']
Since the managed host is remote node, I'm not able to think of lookup and difference filtering. Kindly suggest how to fetch the difference: "SDD"
Adding custom facts is what you're looking for. This serves the very purpose of storing persistent custom facts. For example, create the directory for the custom facts and display the variable ansible_local
The variable ansible_local shall be an empty dictionary if you haven't configured custom facts before
Set the variables before_add and after_add, and display the difference
When you run the play for the first time the variable before_add will be an empty list and the debug task should display all devices, e.g.
Here comes the most important part of storing the persistent custom fact. For example, copy the dictionary into the file
When you run the play again setup should read ansible_local
and there shall be no difference between before_add and after_add
If you add a device the debug shall display the difference, e.g.
But, the difference will be empty if you remove the device. If you want to see both added and removed devices use symmetric_difference instead of difference, e.g.