0

I need to write an Ansible playbook, in which I need to install the software if a specific package is installed. how can I call this information into my playbook, when I run

ansible node1 -m package_facts 

I get the information about packages and my interest in this package.

"java-1.8.0-openjdk": [{
                "arch": "x86_64",
                "epoch": 1,
                "name": "java-1.8.0-openjdk",
                "release": "2.el8",
                "source": "rpm",
                "version": "1.8.0.201.b09"
        

How can I call this package into the playbook, for instance for ip address the path is ansible_facts['default_ipv4']['address']. How I can call this into my package_facts so can I do further actions?

how to map the package_facts package to the playbook?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103

2 Answers2

1

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
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

The package_facts suggests you can just access this through the ansible_facts.packages. They even provide an example

- name: Gather the package facts
  ansible.builtin.package_facts:
    manager: auto

- name: Print the package facts
  ansible.builtin.debug:
    var: ansible_facts.packages

- name: Check whether a package called foobar is installed
  ansible.builtin.debug:
    msg: "{{ ansible_facts.packages['foobar'] | length }} versions of foobar are installed!"
  when: "'foobar' in ansible_facts.packages"
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42