For example, given the inventory
shell> cat hosts
host01
host02
host03
and the list
list:
- host: host01
diskn: disk0
os: 13.0
- host: host02
diskn: disk4
os: 13.0
- host: host06
diskn: hdisk7
os: 13.0
Create the list of the allowed hosts
my_hosts: "{{ list|map(attribute='host')|list }}"
gives
my_hosts:
- host01
- host02
- host06
Next, create a dictionary of the allowed hosts and OS versions
my_os: "{{ list|items2dict(key_name='host', value_name='os') }}"
gives
my_os:
host01: 13.0
host02: 13.0
host06: 13.0
Now, the tasks below remove the file conditionally and display the registered results
- name: Remove file from client
file:
state: absent
path: /tmp/me
when:
- inventory_hostname in my_hosts
- ansible_distribution_version ==
my_os[inventory_hostname]|default('None')|string
register: fileopt
- debug:
var: fileopt
gives
TASK [Remove file from client] *********************************************
skipping: [host03]
ok: [host02]
ok: [host01]
TASK [debug] ***************************************************************
ok: [host01] =>
fileopt:
changed: false
failed: false
path: /tmp/me
state: absent
ok: [host02] =>
fileopt:
changed: false
failed: false
path: /tmp/me
state: absent
ok: [host03] =>
fileopt:
changed: false
skip_reason: Conditional result was False
skipped: true
As expected, host03 which is missing from the list was skipped.
Optionally, creating a dynamic group might be more efficient if the list of the allowed hosts is much smaller compared to all hosts. For example, create a group of allowed hosts in the first play and use it in the second play
- name: Create group my_hosts
hosts: all
gather_facts: true
vars:
list:
- host: host01
diskn: disk0
os: 13.0
- host: host02
diskn: disk4
os: 13.0
- host: host06
diskn: hdisk7
os: 13.0
my_hosts: "{{ list|map(attribute='host')|list }}"
my_os: "{{ list|items2dict(key_name='host', value_name='os') }}"
tasks:
- set_fact:
_my_os: "{{ ansible_distribution_version ==
my_os[inventory_hostname]|default('None')|string }}"
- name: Create group my_hosts
add_host:
name: "{{ item }}"
groups: my_hosts
loop: "{{ hostvars|dict2items|
selectattr('key', 'in', my_hosts)|
selectattr('value._my_os')|
map(attribute='key')|
list }}"
run_once: true
- name: Remove file from my_hosts
hosts: my_hosts
gather_facts: false
tasks:
- name: Remove file from client
file:
state: absent
path: /tmp/me
register: fileopt
- debug:
var: fileopt
gives
PLAY [Create group my_hosts] ***************************************
TASK [Gathering Facts] *********************************************
ok: [host03]
ok: [host01]
ok: [host02]
TASK [set_fact] ****************************************************
ok: [host03]
ok: [host02]
ok: [host01]
TASK [Create group my_hosts] ***************************************
ok: [host01] => (item=host01)
ok: [host01] => (item=host02)
PLAY [Remove file from my_hosts] ***********************************
TASK [Remove file from client] *************************************
ok: [host01]
ok: [host02]
TASK [debug] *******************************************************
ok: [host01] =>
fileopt:
changed: false
failed: false
path: /tmp/me
state: absent
ok: [host02] =>
fileopt:
changed: false
failed: false
path: /tmp/me
state: absent