ansible shell 模块

文章目录

shell 模块

官网参考地址:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html

切换到某个 shell 执行指定的指令,与 command 不同的是,此模块可以支持命令管道,同时还有另一个模块也具备此功能:raw

了解模块用法的渠道:

  • 使用 ansible-doc -s shell 查看帮助文档
  • 查看官网地址学习
  • 官网不好看,多去学习别人的文档
[root@master ~]# ansible-doc -s shell
- name: Execute shell commands on targets
  shell:
      chdir:                 # Change into this directory before running the command.
      cmd:                   # The command to run followed by optional arguments.
      creates:               # A filename, when it already exists, this step will *not* be run.
      executable:            # Change the shell used to execute the command. This expects an absolute path to the executable.
      free_form:             # The shell module takes a free form command to run, as a string. There is no actual parameter named 'free form'. See the examples on how to use this module.
      removes:               # A filename, when it does not exist, this step will *not* be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      stdin_add_newline:     # Whether to append a newline to stdin data.
      warn:                  # Whether to enable task warnings.
      

参数解析:

参数说明
chdir在执行命令之前,通过 cd 进入到该参数指定的目录
creates在创建一个文件之前,先判断文件是否存在,如果存在则跳过前面的东西,如果不存在则执行前面的动作
free_form该参数即为输入的 linux 系统命令,实现远程执行和管理
removes判断一个文件是否存在,如果存在则执行前面的动作,如果不存在则跳过前面的内容(和上面的 creates 是相反的)
warn是否提供告警信息
  • 示例1:

查看被管理机器上的进程信息

[root@master ~]# ansible dong -m shell -a "ps -ef | grep ssh | grep -v grep"
192.168.169.162 | CHANGED | rc=0 >>
root        900      1  0 10:42 ?        00:00:00 /usr/sbin/sshd -D
root       1014    900  0 10:44 ?        00:00:00 sshd: root@pts/0
root       1021    900  0 10:44 ?        00:00:00 sshd: root@notty
root       1035   1021  0 10:44 ?        00:00:00 /usr/libexec/openssh/sftp-server
root      10856    900  3 14:05 ?        00:00:00 sshd: root@pts/1
192.168.169.161 | CHANGED | rc=0 >>
root        899      1  0 10:42 ?        00:00:00 /usr/sbin/sshd -D
root       1011    899  0 10:45 ?        00:00:00 sshd: root@pts/0
root       1014    899  0 10:45 ?        00:00:00 sshd: root@notty
root       1032   1014  0 10:45 ?        00:00:00 /usr/libexec/openssh/sftp-server
root      11454    899  0 14:05 ?        00:00:00 sshd: root@pts/1

  • 示例2:

批量远程执行脚本,需要执行的脚本要求必须在客户端存在,否则会报错文件不存在。这个是 shell 模块的特点,是因为还有另外一个专门执行脚本的 script 模块

操作步骤:
1.创建文件夹
2.创建 shell 脚本,写入内容
3.赋予脚本可执行权限
4.执行脚本
5.忽略 warning 信息
以上操作均需要在管理机器上操作,类似这种比较复杂的命令可以使用 playbook 剧本模式去编写

[root@master ~]# ansible dong -m shell -a "mkdir /data;echo 'hostname' > /data/hostname.sh;chmod u+x /data/hostname.sh;sh /data/hostname.sh warn=false creates=/data"
192.168.169.161 | CHANGED | rc=0 >>
node01
192.168.169.162 | CHANGED | rc=0 >>
node02

 
 
 
 
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值