Ansible ping模块
Ansible最基础的模块为ping模块,主要用于判断远程客户端是否在线,用于ping本身服务器,返回值为change、ping。
Ansible ping模块企业常用案例如下:
1.使用Ansible ping检测管理的主机是否存活
[root@hl-web ~]# ansible web -m ping
192.168.33.30 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.33.50 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.33.40 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
Ansible command模块
Ansible command模块为Ansible默认模块,主要用于执行Linux基础命令,可以执行远程服务器命令执行,任务执行等操作。command模块使用详解如下:
- Chdir:执行命令前,切换到目录
- Creates:当该文件存在时,则不执行该步骤
- Executable:换用shell环境执行命令
- Free_form:需要执行的脚本
- Removes:当该文件存在时,则执行该步骤
- Warn:若在ansuble.cfg中存在告警,如果设定了false,不会告警此行
Ansible command模块企业常用案列如下:
1.Ansible command模块远程执行date命令
[root@hl-web ~]# ansible web -m command -a 'date'
192.168.33.40 | CHANGED | rc=0 >>
Wed Sep 4 17:04:34 CST 2019
192.168.33.50 | CHANGED | rc=0 >>
Wed Sep 4 17:04:34 CST 2019
192.168.33.30 | CHANGED | rc=0 >>
Wed Sep 4 17:04:34 CST 2019
2.Ansible command模块远程执行ping命令
[root@hl-web ~]# ansible 192.168.33.30 -m command -a "ping -c 1 www.baidu.com"
3.Ansible hosts正则模式远程执行df -h
[root@hl-web ~]# ansible 192.168.33.?? -m command -a 'df -h'
注:有些符号信息无法识别:“<”,“>”,“|”,“;”,“&”
Ansible shell模块
Ansible shell模块主要用于远程客户端上执行各种shell命令或运行脚本,远程执行命令通过/bin/sh环境来执行,支持比command更多的指令,shell模块使用详解如下:
- Chdir:执行命令前,切换到目录
- Creates:当该文件存在时,则不执行该步骤
- Excutable:换用shell环境执行命令
- Free_form:需要执行的脚本
- Removes:当该文件不存在时,则不执行该步骤
- Warn:如果在ansible.cfg中存在告警,如果设定了False,不会告警此行
Ansible shell模块企业常用案例:
1.远程执行shell脚本,并把执行结果追加到客户端服务器/tmp/var.log文件
[root@hl-web ~]# ansible 192.168.33.30 -m shell -a "/bin/sh /tmp/variables.sh >> /tmp/var.log"
2.远程执行创建目录命令,执行之前切换在/tmp目录,屏蔽告警信息
[root@hl-web ~]# ansible 192.168.33.30 -m shell -a "mkdir -p 1/2 chdir=/tmp/ state=directory warn=no"
3.用管道|远程查看http进程是否启动
[root@hl-web ~]# ansible 192.168.33.30 -m shell -a "ps ef | grep http"
Ansible copy模块
Ansible copy模块主要用于文件或目录复