简体   繁体   中英

passing a list to a role in Ansible

I need to read from a config file which needs to contain a list. The list needs to be passed to a role as argument.

---
- name: run command on localhost
  hosts: localhost
  tasks:
    - name: read variables from file
    shell: cat {{ conf1/TMP.txt }}
    register: contents
- name: role to trigger the run script process
  hosts: otherhost
  roles:
    - { role: run_script, applist: "{{ contents }}"  }

The content of the conf1/TMP.txt file is as follows:

[ 'a', 'b', 'c', 'd' ]

The above mentioned code segment is not working but the following code segment works:

---
- name: main yml file to trigger the whole process
  hosts: otherhost
  roles:
   - { role: run_script, applist: [ 'a', 'b', 'c', 'd' ] }

Try using a lookup filter instead of a shell command. Example below..

---
- name: run command on localhost
  hosts: localhost

  tasks:
    - set_fact:
        contents: "{{ lookup('file', 'tmp.txt')  }}"
    - debug: var=contents

- name: role to trigger the run script process
  hosts: localhost
  roles:
    - { role: foo, applist: "{{ contents  }}" }

The output of debug should look like this

ok: [localhost] => {
"foo": [
    "1",
    "2",
    "3",
    "4"
]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM