[英]How to pass extra variables to ansible as list of dictionary to ansible playbook by --extra-vars?
I want to pass variables to my ansible playbook by --extra-vars, and the type of variables is a list of dict like this: 我想通过--extra-vars将变量传递给我的ansible剧本,变量的类型是这样的字典列表:
list1: - { key1: "val1", key2: "val2" } - { key1: "val3", key2: "val4" }
My playbook is : 我的剧本是:
--- - name: main file gather_facts: false hosts: localhost vars: list1: "{{ lists }}" tasks: - name: echo item shell: echo {{ item.key1 }} with_items: list1
and I try to pass variables like this: 我尝试传递这样的变量:
ansible-playbook build_and_cppcheck.yml -e "lists=[{ "key1": "val1", "key2":"val2" },{ "key1": "val3", "key2":"val4" }]"
fatal: [localhost] => with_items expects a list or a set
Just use JSON string syntax: Ansible doc . 只需使用JSON字符串语法: Ansible doc即可 。 For example: 例如:
$ play.yml $ play.yml
---
- hosts: localhost
gather_facts: no
tasks:
- debug:
msg: "This is {{ test[0] }}"
- debug:
msg: "This is {{ test[1] }}"
$ ansible-playbook play.yml -e '{"test":["1.23.45", "12.12.12"]}' $ ansible-playbook play.yml -e'{“ test”:[“ 1.23.45”,“ 12.12.12”]}'
[3sky@t410 testing]$ ansible-playbook play.yml -e '{"test":["1.23.45", "12.12.12"]}'
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] ********************************************************************************
TASK [debug] ********************************************************************************
ok: [localhost] => {
"msg": "This is 1.23.45"
}
TASK [debug] ********************************************************************************
ok: [localhost] => {
"msg": "This is 12.12.12"
}
PLAY RECAP ********************************************************************************
localhost
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.