- name: Execute commands in nodes.
action: shell
chdir # cd into this directory before running the command
creates # a filename, when it already exists, this step will *not* be run.
executable # change the shell used to execute the command. Should be an absolute path to the executable.
free_form= # The shell module takes a free form command to run, as a string. There's not an actual option named "free form". See the
examples!
removes # a filename, when it does not exist, this step will *not* be run.
warn # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false.
说明
chdir:在哪个目录下执行shell命令,相当于执行shell命令前先cd到对应目录
creates:如果存在某文件,就不执行shell
removes:如果不存在某文件,就不执行shell;与creates左右相反(有点绕)
executable:修改并指定shell解释器来执行命
free_form:指的就是具体的shell命令,实际上是一个不存在的选项
例子
/etc/ansible/hosts配置如下:
[local]
127.0.0.1
1) 查看当前目录,在local节点上运行
#-m shell表示使用shell模块
#-a 表示传递shell模块的相关参数
#-i 指定了inventory文件
#local是inventory文件里面定义好的节点
ansible -i /etc/ansible/hosts local -m shell -a 'pwd'
ansible常用模块介绍:shell简介如何使用
2) 设定/root/ccg为当前目录,在local节点上运行
ansible -i /etc/ansible/hosts local -m shell -a 'pwd chdir=/root/ccg
'
ansible常用模块介绍:shell简介如何使用
也可以写成:ansible -i /etc/ansible/hosts local -m shell -a 'cd /root/ccg && pwd'
3) 使用shell解释器为dash执行
ansible -i /etc/ansible/hosts local -m shell -a 'pwd executable=/bin/dash'
ansible常用模块介绍:shell简介如何使用
4) 如果存在~/.ssh/id_rsa则不执行该shell命令
ansible -i /etc/ansible/hosts local -m shell -a 'pwd creates=~/.ssh/id_rsa'
ansible常用模块介绍:shell简介如何使用
5)如果存在~/.ssh/id_rsa则执行该shell命令
ansible -i /etc/ansible/hosts local -m shell -a 'pwd remove=~/haha'