[英]How to pass a multiline JSON string to ansible-playbook's --extra-vars
[英]How to pass extra variables to ansible as list of dictionary to ansible playbook by --extra-vars?
我想通過--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
只需使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.