Chakaravarthy Natesan
Chakaravarthy Natesan

Reputation: 95

ansible with_items pass variables from outside file

I just want to pass list of rpm packages in a yml file and call it in with_items inside my tasks. Which format the yml file should be. Please help me. I googled a lot, still being confused. I need to achieve so that I could change only the package names in outside file, without changing the main file.

Ex: files.yaml

---
- vars:
    modules:
      - firmware-system-p89-2.56_2018_01_22-1.1.i386.rpm
      - firmware-smartarray-ea3138d8e8-6.30-1.1.x86_64.rpm

=> passing to with_items in another file

---
- name: List required packages
  include_vars:
    -files.yml
  set_fact: pkglist="{{ item}}"
  with_items:
    - "{{ modules }}"
  register: pkglist_result


    Comment:

Thanks a lot. that helps. I did just this to finally accomplish. I wasn't trying right. But I mentioned just the name of the packages in files.yml and placed the real packages in files directory where tasks directory resides.

- name: List required packages
  include_vars: files.yml
  register: pkglist_result

- name: make a list
  set_fact: pkg_list="{{ pkglist_result.ansible_facts.modules}}"

- debug: var=pkg_list

Upvotes: 8

Views: 26617

Answers (2)

Franck Mercado
Franck Mercado

Reputation: 179

This is an alternative you can do by using lookup (more info).

with_items: "{{ lookup('file', '../files/mylist.json') }}"

Upvotes: 0

techraf
techraf

Reputation: 68469

files.yml:

---
modules:
  - firmware-system-p89-2.56_2018_01_22-1.1.i386.rpm
  - firmware-smartarray-ea3138d8e8-6.30-1.1.x86_64.rpm

example playbook:

---
- hosts: my_hosts
  vars_files:
    - files.yml
  tasks:
    - name: print module name one by one
      debug:
        msg: "{{ item }}"
      with_items: "{{ modules }}"

Upvotes: 21

Related Questions