gpt4 book ai didi

ansible - 如何通过 --extra-vars 将额外变量作为字典列表传递给 ansible 到 ansible playbook?

转载 作者:行者123 更新时间:2023-12-02 00:26:54 51 4
gpt4 key购买 nike

我想通过 --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 .例如:

$播放.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

关于ansible - 如何通过 --extra-vars 将额外变量作为字典列表传递给 ansible 到 ansible playbook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53447147/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com