Q: "How can I call a package ip address?"
A: Get the lists of IP addresses and packages, and create a dictionary. For example,
ips: "{{ hostvars|json_query('*.ansible_all_ipv4_addresses[0]') }}"
pkg: "{{ hostvars|json_query('*.packages') }}"
ips_pkg: "{{ dict(ips|zip(pkg)) }}"
Fit the IP list to your needs. For example,
ips: "{{ hostvars|json_query('*.ansible_default_ipv4.addresses') }}"
Create a list of what you want to search for. For example,
search_list:
- {ip: 10.1.0.61, pkg: gtar}
- {ip: 10.1.0.63, pkg: gtar}
Use the dictionary ips_pkg to find the packages
- debug:
msg: "{{ ips_pkg[item.ip][item.pkg] }}"
loop: "{{ search_list }}"
run_once: true
gives
TASK [debug] **********************************************************************************
ok: [test_11] => (item={'ip': '10.1.0.61', 'pkg': 'gtar'}) =>
msg:
- arch: amd64
automatic: false
category: archivers
installed: '1643011884'
name: gtar
origin: FreeBSD
port_epoch: 0
prefix: /usr/local
revision: '0'
source: pkg
version: '1.34'
vital: false
ok: [test_11] => (item={'ip': '10.1.0.63', 'pkg': 'gtar'}) =>
msg:
- arch: amd64
automatic: false
category: archivers
installed: '1643004494'
name: gtar
origin: FreeBSD
port_epoch: 0
prefix: /usr/local
revision: '0'
source: pkg
version: '1.34'
vital: false
Example of a complete playbook for testing
- hosts: all
vars:
ips: "{{ hostvars|json_query('*.ansible_all_ipv4_addresses[0]') }}"
pkg: "{{ hostvars|json_query('*.packages') }}"
ips_pkg: "{{ dict(ips|zip(pkg)) }}"
search_list:
- {ip: 10.1.0.61, pkg: gtar}
- {ip: 10.1.0.63, pkg: gtar}
tasks:
- setup:
gather_subset: network
- debug:
var: ansible_all_ipv4_addresses.0
- package_facts:
- debug:
var: packages.gtar
- debug:
msg: "{{ ips_pkg[item.ip][item.pkg] }}"
loop: "{{ search_list }}"
run_once: true