這是 ansible-playbook 的示例,如果我想通過終端的 ad-hoc 命令傳遞 {{ item.first }} 和 {{ item.second }} 的值。
我們怎么做?
提前致謝..
---
- hosts: localhost
tasks:
- name: Here we are providing a list which have items containing multiple
debug:
msg: "current first value is {{ item.first }} and second value is {{ item.second }}"
with_items:
- { first: lemon, second: carrot }
- { first: cow, second: goat }
uj5u.com熱心網友回復:
因此,要debug通過從命令列(或其他任何地方)傳遞值來回圈您的任務,您需要為任務使用一個變數(例如with_items: "{{ my_var }}")。
有很多方法可以為變數提供值,但對于這個特定要求,我們可以使用--extra-vars.
考慮一個劇本myplaybook.yml如下:
- hosts: localhost
tasks:
- name: Here we are providing a list which have items containing multiple
debug:
msg: "current first value is {{ item.first }} and second value is {{ item.second }}"
with_items: "{{ my_var }}"
可以通過在命令列上傳遞這個變數來運行:
ansible-playbook myplaybook.yml -e '{my_var: [{first: lemon, second: carrot}, {first: cow, second: goat}]}'
請注意使用可以提供哪些變數的其他方式(上面鏈接),如果它們比這種方法更有效,請使用它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/396327.html
標籤:变量 能听懂的 ansible 模块 ansible-ad-hoc
下一篇:訪問同一類的嵌套函式中的變數
