ansible常用模块介绍:shell
简介
shell模块是ansible里面比较常用的模块。顾名思义,shell模块用于执行shell命令。我们利用ansible批量在多台机器上执行shell命令,该模块非常的简单粗暴。
对于ansible很多模块,都可以用shell替代达到相对应的效果。
虽然shell模块看起来非常万能,但存在着许多风险。例如:shell注入风险,非幂等性(即统一命令多次执行的结果可能是不一样的)
如何使用
帮助
ansible-doc -s shell- 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'
2) 设定/root/ccg为当前目录,在local节点上运行
ansible -i /etc/ansible/hosts local -m shell -a 'pwd chdir=/root/ccg'
也可以写成: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'
4) 如果存在~/.ssh/id_rsa则不执行该shell命令
ansible -i /etc/ansible/hosts local -m shell -a 'pwd creates=~/.ssh/id_rsa'
5)如果存在~/.ssh/id_rsa则执行该shell命令
ansible -i /etc/ansible/hosts local -m shell -a 'pwd remove=~/haha'
以上就是ansible的shell模块的解释,后面会带来command,raw,script模块的使用分享。
博主:测试生财
座右铭:专注测试与自动化,致力提高研发效能;通过测试精进完成原始积累,通过读书理财奔向财务自由。
csdn:https://blog.csdn.net/ccgshigao
- 利用数据库拿shell的一些姿势
- 够用的 shell 脚本
- 用 shell 脚本做 restful api 接口监控
- PHP执行普通shell命令流程解析
- Bash技巧:把变量赋值为换行符(判断文件是否以换行符结尾)
- 详解shell 变量
- 浅谈Shell中的函数
- BeanShell PreProcessor数组参数
- 善用Bash history 命令
- 最流行的shell,bash详细介绍及入门
- Shell笔记1-bash数学运算
- 反弹shell的方法总结
- 使用shell脚本查询主机cpu、内存、硬盘使用情况(显示时间,IP,使用情况等)。
- shell使用for、while循环编写脚本计算100以内所有能被3整除的整数之和
- shell脚本使用for、while循环编写脚本实现连续自然数的和,自然数的个数可以在执行脚本过程中自行定义,并分别输出最后结果。
- shell小练习:编写脚本检测192.168.1.0/24网段中在线的主机,将在线的主机和不在线主机的IP地址分别记录在不同的文件中。
- SHELL脚本编程
- shell自动处理文件内部操作
- shell 经典传值案例
- go shell执行优化版+ 输出结果为table格式