Ansible常用模块

ping模块:ping模块用于检查故障回复机器是否连通,常用很简单,不知道,主机是否在线,则pong
  ```
  [root@localhost ansible]# ansible all -m ping
  192.168.145.162 | SUCCESS => {
  "ansible_facts": {
  "discovered_interpreter_python": "/usr/bin/python"
  }, 
  "changed": false, 
  "ping": "pong"
  }
命令模块:命令模块用于在远程主机上执行命令,ansible默认就是使用命令模块。
  命令模块有一个缺陷就是不能使用管道符和执行功能。
  ```
  查看受控主机的/tmp目录内容
  [root@localhost ansible]# ansible 192.168.145.162 -a ‘ls /tmp‘
  192.168.145.162 | CHANGED | rc=0 >>
  anaconda.log
  ansible_command_payload_VyRlDG
  hsperfdata_root
  ifcfg.log
  ks-script-_gcBHe
  packaging.log
  program.log
  sensitive-info.log
  ssh-6fGPSTTiKzXF
  ssh-xzhjRraNfxN4
  storage.log
  systemd-private-690395c778184bfa90bf660790ea8668-chronyd.service-70oYja
  systemd-private-690395c778184bfa90bf660790ea8668-colord.service-biAHyz
  systemd-private-690395c778184bfa90bf660790ea8668-cups.service-5QFOST
  systemd-private-690395c778184bfa90bf660790ea8668-rtkit-daemon.service-jSzsX1
  systemd-private-da3b33eea333443190bdef1c7a7a2f4e-chronyd.service-wVm7qf
  systemd-private-da3b33eea333443190bdef1c7a7a2f4e-colord.service-iEEarO
  systemd-private-da3b33eea333443190bdef1c7a7a2f4e-cups.service-uOT36H
  systemd-private-da3b33eea333443190bdef1c7a7a2f4e-rtkit-daemon.service-ccniPF
  tracker-extract-files.0
  vmware-root
  yum.log
  [root@localhost ansible]# 
  
  在受控主机的/tmp目录下新建一个文件test
  [root@localhost ansible]# ansible 192.168.145.162 -a ‘touch /tmp/test‘
  [WARNING]: Consider using the file module with state=touch rather than running
  ‘touch‘.  If you need to use command because file is insufficient you can add ‘warn:
  false‘ to this command task or set ‘command_warnings=False‘ in ansible.cfg to get
  rid of this message.
  192.168.145.162 | CHANGED | rc=0 >>
  
  [root@localhost ansible]# ansible 192.168.145.162 -a ‘ls /tmp‘
  192.168.145.162 | CHANGED | rc=0 >>
  anaconda.log
  ansible_command_payload_sjrmKi
  hsperfdata_root
  ifcfg.log
  ks-script-_gcBHe
  packaging.log
  program.log
  sensitive-info.log
  ssh-6fGPSTTiKzXF
  ssh-xzhjRraNfxN4
  storage.log
  systemd-private-690395c778184bfa90bf660790ea8668-chronyd.service-70oYja
  systemd-private-690395c778184bfa90bf660790ea8668-colord.service-biAHyz
  systemd-private-690395c778184bfa90bf660790ea8668-cups.service-5QFOST
  systemd-private-690395c778184bfa90bf660790ea8668-rtkit-daemon.service-jSzsX1
  systemd-private-da3b33eea333443190bdef1c7a7a2f4e-chronyd.service-wVm7qf
  systemd-private-da3b33eea333443190bdef1c7a7a2f4e-colord.service-iEEarO
  systemd-private-da3b33eea333443190bdef1c7a7a2f4e-cups.service-uOT36H
  systemd-private-da3b33eea333443190bdef1c7a7a2f4e-rtkit-daemon.service-ccniPF
  test //
  tracker-extract-files.0
  vmware-root
  yum.log
  
  command模块不支持管道符,不支持重定向
  [root@localhost ansible]# ansible 192.168.145.162 -a "echo ‘hello world‘ > /tmp/test"
  192.168.145.162 | CHANGED | rc=0 >>
  hello world > /tmp/test
  
  [root@localhost ansible]# ansible 192.168.145.162 -a ‘cat /tmp/test‘
  192.168.145.162 | CHANGED | rc=0 >>

  [root@localhost ansible]# ansible 192.168.145.162 -a ‘ps -ef|grep vsftpd‘
  192.168.145.162 | FAILED | rc=1 >>
  error: unsupported SysV option

  Usage:
  ps [options]

  Try ‘ps --help <simple|list|output|threads|misc|all>‘
  or ‘ps --help <s|l|o|t|m|a>‘
  for additional help text.

  For more details see ps(1).non-zero return code
 
raw模块:raw模块用于在远程主机上执行命令,其支持管道符与生成
  ```
  支持重定向
  [root@localhost ansible]# ansible 192.168.145.162 -m raw -a ‘echo "hello world" > /tmp/test‘
  192.168.145.162 | CHANGED | rc=0 >>
  Shared connection to 192.168.145.162 closed.

  [root@localhost ansible]# ansible 192.168.145.162 -a ‘cat /tmp/test‘
  192.168.145.162 | CHANGED | rc=0 >>
  hello world

  支持管道符
  [root@localhost ansible]# ansible 192.168.145.162 -m raw -a ‘cat /tmp/test|grep -Eo hello‘
  192.168.145.162 | CHANGED | rc=0 >>
  hello
  Shared connection to 192.168.145.162 closed.

shell模块:shell 模块用于在控制机上执行管理机上的脚本,自行在控制机上执行命令。
  ```
  查看受控机上的脚本
  [root@localhost ~]# cd /tmp/
  [root@localhost tmp]# vi test.sh
  [root@localhost tmp]# chmod +x test.sh
  [root@localhost tmp]# ll
  -rwxr-xr-x. 1 root root   31 7月  18 21:02 test.sh
        
  [root@localhost ansible]# ansible 192.168.145.162 -m shell -a ‘/tmp/test.sh‘
  192.168.145.162 | CHANGED | rc=0 >>
  hello word
  
  [root@localhost ansible]# ansible 192.168.145.162 -m shell -a ‘echo "jjyy" > /tmp/test.sh‘
  192.168.145.162 | CHANGED | rc=0 >>
  [root@localhost ~]# cat /tmp/test.sh
  jjyy
 
yum模块:yum 模块用于在指定节点机器上通过 yum 管理软件,其支持的参数主要有两个
    
   name:要管理的包名
   state:要进行的操作
   状态常用的值:
   
   最新:安装软件
   安装:安装软件
   当前:安装软件
   移除:卸软件
   缺席:卸软件
   如果想使用 yum 来管理软件,请确保执行机器上的 yum 源无异常。
   ```
   在受控机上查询看vsftpd软件是否安装
   [root@localhost ~]# rpm -qa|grep vsftpd
   [root@localhost ~]# 
   
   在ansible主机上使用yum模块在受控机上安装vsftpd
   [root@localhost ansible]# ansible 192.168.145.162 -m yum -a ‘name=vsftpd state=present‘
   192.168.145.162 | CHANGED => {
   "ansible_facts": {
   "discovered_interpreter_python": "/usr/bin/python"
   }, 
   "changed": true, 
   "changes": {
   "installed": [
       "vsftpd"
   ]
   }
   
   查看受控机上是否安装了vsftpd
   [root@localhost ~]# rpm -qa|grep vsftpd
   vsftpd-3.0.2-29.el7_9.x86_64

Ansible常用模块

上一篇:React的一些基本介绍


下一篇:Flask 框架