-1

I tried to implement a new YML to list and give me the common packages in my 60 plus servers.

Here is the draft:

- name: Save list of common packages
  hosts: all
  gather_facts: false
  tasks:
    - name: Get list of installed packages
      shell: dpkg-query -W -f="\${Package} \${Version}\n" | sort
      register: packages

    - name: Set fact for list of installed packages
      set_fact:
        installed_packages: "{{ packages.stdout_lines }}"

    - name: Add installed packages to group_vars
      add_host:
        name: "{{ inventory_hostname }}"
        installed_packages: "{{ installed_packages }}"

    - name: Get list of installed packages across all servers
      shell: "grep installed_packages group_vars/* | cut -d: -f3- | sort -u | xargs -n 1 echo | sort | uniq -c | grep '^\s*{{ groups['all'][0] }}' | awk '{$1=\"\"; print $0}'"
      register: common_packages
      delegate_to: localhost

    - name: Print list of common packages
      debug:
        var: common_packages.stdout_lines

    - name: Save list of common packages to file
      copy:
        content: "{{ common_packages.stdout }}"
        dest: "/resultdpkg/result.txt"
        delegate_to: localhost

It seems to work but nothing displayed in the file result. Can you help?

List all commons dpkg in all my servers and print / save the result in a file in the "bastion".

U880D
  • 8,601
  • 6
  • 24
  • 40
Galileo59
  • 1
  • 1

2 Answers2

1

I don't have your group_vars/* file but I see two small errors:

  1. The last delegate_to should be in the copy as below:
    - name: Save list of common packages to file
      copy:
        content: "{{ common_packages.stdout }}"
        dest: "/resultdpkg/result.txt"
      delegate_to: localhost
  1. When I execute on my machine I have a grep error I have to replace ... grep '^\s* ... by ... grep '^\\s* ....

Here is a version "Simplify that works on my side" :

- name: Save list of common packages
  connection: local
  hosts: all
  gather_facts: false
  tasks:
    - name: Get list of installed packages
      shell: dpkg-query -W -f="\${Package} \${Version}\n" | sort
      register: packages

    - name: Print list of common packages
      debug:
        var: common_packages.stdout_lines
   
    - name: Save list of common packages to file
      copy:
        content: "{{ packages.stdout }}"
        dest: "/tmp/result.txt"

I think the problem is with your grep.

Hope to help you :)

1

How to list and give me the common packages in my 60 plus servers?

It is recommended to use the package_facts module in order to get package information as facts.

A minimal Example playbook

---
- hosts: test
  become: false
  gather_facts: false

  tasks:

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

  - name: Print the package names only
    ansible.builtin.debug:
      msg: "{{ ansible_facts.packages | dict2items | map(attribute='key') }}"

will result into a list output of the installed packages.

Further Reading

And more can be found by search ...

U880D
  • 8,601
  • 6
  • 24
  • 40