我想通过--extra-vars将变量传递给我的ansible剧本,并且变量的类型是这样的字典列表:
list1:
- { key1: "val1", key2: "val2" }
- { key1: "val3", key2: "val4" }
我的剧本是:
---
- name: main file
gather_facts: false
hosts: localhost
vars:
list1: "{{ lists }}"
tasks:
- name: echo item
shell: echo {{ item.key1 }}
with_items: list1
我尝试传递这样的变量:
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
答案 0 :(得分:0)
只需使用JSON字符串语法:Ansible doc。例如:
$ 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”]}'
[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