0

Is there a way to pass a list of values to /vars/main.yml file to subsequently pass those variables to the tasks for processing.

The intention here is to running the task once rather than N times.

My current /vars/main.yml

---
srv:
   httpd
   nginx

parses as TASK [app : Install the httpd nginx server package] ************ which is not liked by yum module.

---
    - name: Install the {{ srv }} server package
      yum:
        name: "{{ srv }}"
        state: present
    - name: Check for the installed package
      yum:
        list: "{{ srv }}"
      register: yumout
    - debug:
       var: yumout
2
  • 1
    I'm not sure what your question is... FIrst of all, your list of values in srv is not formatted with correct ansible yaml. You need to indicate each with a -. Second, the tasks are calling "{{ srv }}" as if it was a single value, but it contains a list of values and it can't handle that. The way you iterate over a list contained in a variable is with loop:
    – 0xSheepdog
    Feb 27, 2020 at 1:23
  • I am trying to pass a list of values to the vars in role, this way i am willing to install multiple packages ( passed as variable ) in one go rather than running the playbook multiple times for different packages
    – Atul
    Feb 27, 2020 at 1:26

2 Answers 2

0

The playbook might look this way. I have not tested the code, it's just off the cuff...

---
- hosts: all
  tasks:
  - name: Install the {{ srv }} server package
    yum:
      name: "{{ item }}"
      state: present
    loop: "{{ srv }"
  - name: Check for the installed package
    yum:
      list: "{{ srv }}"
    loop: "{{ srv }"
    register: yumout
  - debug:
    msg: yumout
1
  • Thank you, will try it out
    – Atul
    Feb 27, 2020 at 1:31
0

Have a look at this example in the documentation of the yum module:

- name: ensure a list of packages installed
  yum:
    name: "{{ packages }}"
  vars:
    packages:
    - httpd
    - httpd-tools

You only have to change the list in your /vars/main.yml:

---
srv:
  - httpd
  - nginx

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .