在 Ansible 中执行带参数的 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45424714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute a bash script with arguments in Ansible
提问by masteraravind
I am new to Ansible. I have a bash script which has three arguments to be passed. I have to run this bash script on the remote server from Ansible.
我是 Ansible 的新手。我有一个 bash 脚本,它有三个要传递的参数。我必须从 Ansible 在远程服务器上运行这个 bash 脚本。
Basically, I want to declare the hostname, duration and the comment fields as arguments while executing the Ansible command. I don't want to edit the file, as I am doing it from a Slack channel.
基本上,我想在执行 Ansible 命令时将主机名、持续时间和注释字段声明为参数。我不想编辑文件,因为我是从 Slack 频道做的。
- hosts: nagiosserver
tasks:
- name: Executing a script
command: sh /home/aravind/downtime.sh {hostname} {duration} {comments}
回答by Hammett
If you're executing ansible via ansible-playbook myplay.yml
, you can pass additional variables via -e varname=varvalue
. A lazy fix would be to run with
如果您通过 执行 ansible ansible-playbook myplay.yml
,则可以通过-e varname=varvalue
. 一个懒惰的解决方法是运行
ansible-playbook myplay.yml -e my_hostname=foo -e my_duration=bar -e my_comments=foobar
But you should consider that the hostname is already defined in your inventory or gathered facts.
但是您应该考虑主机名已经在您的清单或收集的事实中定义。
So you could update your playbook to use these additional variables using
所以你可以更新你的剧本以使用这些额外的变量
- hosts: nagiosserver
tasks:
- name: Executing a script
- command: "sh /home/aravind/downtime.sh {{my_hostname}} {{my_duration}} {{my_comments}}"