Papouche Guinslyzinho
Papouche Guinslyzinho

Reputation: 5458

How to print package names that are being install with a loop in Ansible.?

I would like to print the package names each time ansible do a loop

  - name: Install base packages
    package:
      name: "{{ packages }}"
      state: present
    vars:
      packages:
        - git
        - vim
        - htop
    register: echo
    debug: "{{packages}}"
    when: ansible_pkg_mgr == 'apt'

Upvotes: 0

Views: 737

Answers (2)

seshadri_c
seshadri_c

Reputation: 7340

Actually your task definition is not looping through the packages. The name parameter can take a list of packages (which is preferred), and that is what you are passing as packages. If you would like to loop and have each package installed iteratively, you should have a loop with loop: {{ packages }}.

Something like below:

  - name: Install base packages
    package:
      name: "{{ item }}"
      state: present
    loop: "{{ packages }}"
    vars:
      packages:
        - git
        - vim
        - htop
    when: ansible_pkg_mgr == 'apt'

Now, every time the task "loops", the name of the item, i.e. the package name (e.g. item=git) will be shown.

Upvotes: 2

Kevin C
Kevin C

Reputation: 5750

The code can be written more efficiently. I've added the debug part so you can see the actual output which happened on the target system.

    - name: Install base packages
      apt:
        pkg:
          - git
          - vim
          - htop
      register: install_pkgs
    when: ansible_pkg_mgr == 'apt'

    - debug:
        msg: "{{ install_pkgs }}"

Upvotes: 1

Related Questions