Pass array in --extra-vars - Ansible

前端 未结 4 1593
遇见更好的自我
遇见更好的自我 2020-12-25 10:02

How can I pass yaml array to --extra-vars in Ansible playbook. Ansible documentation does not declares its syntax nor I can find that on any internet resource.<

4条回答
  •  攒了一身酷
    2020-12-25 10:24

    Perhaps, don't try to pass complex types via command line and handle their creation within the playbook from json files or strings.

    So, @NelonG's approach works but how will it work in all execution contexts? My playbooks tend to get executed by Jenkins jobs via ansiblePlaybook and via packer. Getting the following to work in all of those (even when the command line looks right) doesn't work and can lead to an escaping nightmare.

    ansible -i localhost, all -m debug -a "var=test_list" \
    --extra-vars='{"test_list": [1,2,3]}' 
    

    How about passing in as a string and then splitting via set_fact (note: this only works if you have elements without problematic characters. I have URLs so they are reasonably safe

    ansible .... -e "test_list_csv=1,2,3,http://foo.bar/file.txt"
    

    In the playbook

    name: generate list from string
      set_fact: 
        test_list: "{{ test_list_csv.split(',') | list }}"
    

    I decided to escape from escaping and it seems to work.

提交回复
热议问题