在--extra-vars中传递数组-Ansible [英] Pass array in --extra-vars - Ansible
问题描述
如何在Ansible剧本中将yaml数组传递给--extra-vars
. Ansible文档未声明其语法,我也无法在任何互联网资源上找到它.
我的意思是如果我有一部剧本:
---
- hosts: {{hostName}}
- remote_user: admin
...
那我应该给我的剧本打电话
ansible-playbook DeployWar.yml --extra-vars="hostName=tomcat-webApp"
但是我想在两个服务器(例如tomcat-webApp
和tomcat-all
)上运行此剧本,我想从外部进行控制,即使用--extra-vars
.我试图做的是:
ansible-playbook DeployWar.yml --extra-vars="hostName=[tomcat-webApp, tomcat-all]"
ansible-playbook DeployWar.yml --extra-vars="hostName={tomcat-webApp, tomcat-all}"
ansible-playbook DeployWar.yml --extra-vars="[{hostName: tomcat-webApp}, {hostName: tomcat-all}]"
但是在所有情况下,剧本都无法在我的通话中声明语法错误. 任何帮助表示赞赏.
回答第一个问题如何在Yasible剧本中将yaml数组传递给--extra-vars." 您可以将> json格式的字符串传递给Extra-vars ./p>
这是一个例子剧目:
- hosts: all
gather_facts: no
tasks:
- debug: var=test_list
以及如何将test_list传递给ansible-playbook:
ansible-playbook -c local -i localhost, test.yml --extra-vars='{"test_list": [1,2,3]}'
尽管您可以为主机使用变量,但我还是建议您检查Ansible的主机管理机制,该机制是库存与--limit
选项.
How can I pass yaml array to --extra-vars
in Ansible playbook. Ansible documentation does not declares its syntax nor I can find that on any internet resource.
I mean if I have a playbook:
---
- hosts: {{hostName}}
- remote_user: admin
...
Then I should call my playbook like
ansible-playbook DeployWar.yml --extra-vars="hostName=tomcat-webApp"
But I want to run this playbook on two servers say tomcat-webApp
and tomcat-all
, and I want to control it from out side i.e. using --extra-vars
. What I have tried to do is:
ansible-playbook DeployWar.yml --extra-vars="hostName=[tomcat-webApp, tomcat-all]"
ansible-playbook DeployWar.yml --extra-vars="hostName={tomcat-webApp, tomcat-all}"
ansible-playbook DeployWar.yml --extra-vars="[{hostName: tomcat-webApp}, {hostName: tomcat-all}]"
But in all cases playbook fails declaring a syntax error in my call. Any help appreciated.
To answer your first question "How can I pass yaml array to --extra-vars in Ansible playbook." you can pass in a json formatted string to extra-vars.
Here is an example play:
- hosts: all
gather_facts: no
tasks:
- debug: var=test_list
And how to pass in test_list to ansible-playbook:
ansible-playbook -c local -i localhost, test.yml --extra-vars='{"test_list": [1,2,3]}'
Though you can use a variable for hosts I recommend checking out Ansible's mechanism for host management which is inventory in conjunction with the --limit
option.
这篇关于在--extra-vars中传递数组-Ansible的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!