skip to Main Content

Doing the Linux Patching through Ansible Automation. Once the patching is completed from the JSON output I need to get some details.

Find the below code when I execute the playbook it does not give proper output from the JSON.

Playbook 1

---
- hosts: localhost
  gather_facts: no
  vars:
    contents: "{{ lookup('file', '/ansible/linuxpatch.json') | from_json }}"
  tasks:
    - name: calling file
      debug:
        msg={{ contents.msg | list }}
      register: msg

    - name: update
      set_fact:
        result: "{{ msg | json_query('*.Installed') }}"

Playbook 2

    - name: report update count
      set_fact:
        update_info: "{{ msg | to_json | from_json | json_query(update_info_query) }}"
      vars:
        update_info_query: >
          *[].{
            Installed: Installed,
          }

From the JSON output I need only the Installed details.

JSON Output

{
    "msg": [
        "",
        true,
        [
            "Installed: NetworkManager-tui-1:1.44.0-4.el9_3.x86_64",
            "Installed: NetworkManager-team-1:1.44.0-4.el9_3.x86_64",
            "Installed: NetworkManager-libnm-1:1.44.0-4.el9_3.x86_64",
            "Installed: NetworkManager-1:1.44.0-4.el9_3.x86_64",
            "Installed: libxml2-2.9.13-5.el9_3.x86_64",
            "Installed: policycoreutils-3.5-3.el9_3.x86_64",
            "Installed: kernel-tools-libs-5.14.0-362.18.1.el9_3.x86_64",
            "Installed: kernel-tools-5.14.0-362.18.1.el9_3.x86_64",
            "Installed: kernel-modules-5.14.0-362.18.1.el9_3.x86_64",
            "Installed: kernel-core-5.14.0-362.18.1.el9_3.x86_64",
            "Installed: kernel-5.14.0-362.18.1.el9_3.x86_64",
            "Installed: grub2-tools-minimal-1:2.06-70.el9_3.2.rocky.0.2.x86_64",
            "Installed: grub2-tools-extra-1:2.06-70.el9_3.2.rocky.0.2.x86_64",
            "Installed: grub2-tools-efi-1:2.06-70.el9_3.2.rocky.0.2.x86_64",
            "Installed: grub2-tools-1:2.06-70.el9_3.2.rocky.0.2.x86_64",
            "Installed: kernel-modules-core-5.14.0-362.18.1.el9_3.x86_64",
            "Installed: grub2-efi-x64-1:2.06-70.el9_3.2.rocky.0.2.x86_64",
            "Installed: openssl-libs-1:3.0.7-25.el9_3.x86_64",
            "Installed: openssl-1:3.0.7-25.el9_3.x86_64",
            "Installed: rocky-repos-9.3-1.2.el9.noarch",
            "Installed: rocky-release-9.3-1.2.el9.noarch",
            "Installed: rocky-gpg-keys-9.3-1.2.el9.noarch",
            "Installed: python-unversioned-command-3.9.18-1.el9_3.1.noarch",
            "Installed: python3-libs-3.9.18-1.el9_3.1.x86_64",
            "Installed: python3-3.9.18-1.el9_3.1.x86_64",
            "Installed: gnutls-3.7.6-23.el9_3.3.x86_64",
            "Installed: grub2-common-1:2.06-70.el9_3.2.rocky.0.2.noarch",
            "Installed: selinux-policy-targeted-38.1.23-1.el9_3.1.noarch",
            "Installed: sqlite-libs-3.34.1-7.el9_3.x86_64",
            "Installed: systemd-udev-252-18.el9.0.1.rocky.x86_64",
            "Installed: systemd-rpm-macros-252-18.el9.0.1.rocky.noarch",
            "Installed: selinux-policy-38.1.23-1.el9_3.1.noarch",
            "Installed: systemd-pam-252-18.el9.0.1.rocky.x86_64",
            "Installed: systemd-libs-252-18.el9.0.1.rocky.x86_64",
            "Installed: systemd-252-18.el9.0.1.rocky.x86_64",
            "Installed: tzdata-2024a-1.el9.noarch",
            "Installed: nss-util-3.90.0-4.el9_3.x86_64",
            "Installed: nss-softokn-freebl-3.90.0-4.el9_3.x86_64",
            "Installed: nss-softokn-3.90.0-4.el9_3.x86_64",
            "Installed: python3-firewall-1.2.5-2.el9_3.noarch",
            "Installed: firewalld-filesystem-1.2.5-2.el9_3.noarch",
            "Installed: firewalld-1.2.5-2.el9_3.noarch",
            "Installed: nspr-4.35.0-4.el9_3.x86_64",
            "Removed: NetworkManager-1:1.44.0-3.el9.x86_64",
            "Removed: NetworkManager-libnm-1:1.44.0-3.el9.x86_64",
            "Removed: NetworkManager-team-1:1.44.0-3.el9.x86_64",

2

Answers


  1. Chosen as BEST ANSWER

    It's works in the JSON output file but how to add into the actual playbook.

    When i tried to add into the play book it's give error.


    • hosts: prod become: yes #become_user: root tasks:
      • name: Cleaning yum log file shell: yum clean all

      • name: INstalling the patching yum: name: '*' state: latest register: list

      • debug: msg: "{{ list.values() | list }}" register: msg

      • reboot: reboot_timeout: 3600

      • name: Final Output set_fact: update_info: "{{ msg | map('msg') | json_query('[].Installed') }}"

      • debug: var: installed


  2. The expression below does what you want

        installed: "{{ contents.msg.2 | map('from_yaml') |
                                        json_query('[].Installed') }}"
    

    Example of a complete playbook for testing

    - hosts: localhost
    
      vars:
    
        contents: "{{ lookup('file', 'linuxpatch.json') | from_yaml }}"
        installed: "{{ contents.msg.2 | map('from_yaml') |
                                        json_query('[].Installed') }}"
    
      tasks:
    
        - debug:
            var: installed
    

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search