Ansible Commands modules(command shell script)

 

ansible模块应用语法格式: ansible 主机名称/主机组名称/主机地址信息/all   -m(指定应用的模块信息)   -a(指定动作信息)

 command – Execute commands on targets

command模块(默认模块)
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a hostname
192.168.0.102 | SUCCESS | rc=0 >>
linux-node1.localdomain

chdir 切换目录
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a "chdir=/tmp touch ansible.txt"
[WARNING]: Consider using file module with state=touch rather than running touch

192.168.0.102 | SUCCESS | rc=0 >>

creates 如果指定文件存在,就不执行命令
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a "creates=/tmp touch ansible.txt"
192.168.0.102 | SUCCESS | rc=0 >>
skipped, since /tmp exists  #如果ansible.txt文件已经存在 ,就跳过 不执行

removes 如果文件存在,就执行
[root@linux-node2 ~]# ansible 192.168.0.102 -m command -a "removes=/tmp touch ansible.txt"
[WARNING]: Consider using file module with state=touch rather than running touch

192.168.0.102 | SUCCESS | rc=0 >>  #如果 ansible.txt文件不存在,就不执行

shell – Execute shell commands on targets

shell模块(万能模块): 如果要用特殊符号 > < | & ‘ ‘ 则要用shell 模块, command是不支持管道符之类的

ansible 192.168.0.102 -m shell -a "hostname"
ansible 192.168.0.102 -m shell -a "echo 123 > haha.txt"
ansible 192.168.0.102 -m shell -a "netstat -anptu | grep xxx"

实践使用 利用shell执行脚本
第一个步骤:编写一个脚本
第二个步骤:将脚本发送到远程主机
第三个步骤:将脚本权限进行修改(添加执行权限)
第四个步骤:运行ansible命令执行脚本

1、第一个步骤:编写一个脚本

eg:执行检查磁盘脚本

#!/bin/bash
basedir=$(cd `dirname $0`;pwd)
echo $basedir
diskmax=10 # 磁盘的阈值

function check_max(){
   local disk_size=$1
   if [ $disk_size -ge $diskmax ]
   then
      echo "unhealth"
   else
      echo "health"
   fi
}

function check_disk_info(){
#grep -v 取反 # cut -d '%' -f 1 以%为分隔符选择第一个 #while read disk_size  循环读入每行数据 赋值给disk_size
   df -h | grep -v /dev/loop0 | grep -v /dev/sr0 | grep dev | awk 'NR > 1 {print $5}' | cut -d '%' -f 1 | while read disk_size  
   do
        echo ""
        echo "disk_size=$disk_size%"
        check_max $disk_size
   done
}

check_disk_info

 2、第二个步骤:将脚本发送到远程主机

拷贝脚本之前,先远程创建好准备拷贝过去的文件目录 ,其中远程创建文件目录有两种方式,操作如下:

方式一:shell命令方式

gota@gota-linux61:~$ ansible cluster -m shell -a "mkdir -p /work/ansible"
 [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.  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 thi
s message.
192.168.1.21 | CHANGED | rc=0 >> 192.168.1.20 | CHANGED | rc=0 >> 192.168.1.63 | CHANGED | rc=0

方式二:file命令执行创建文件夹

gota@gota-linux61:~$ ansible cluster -m file -a "path=/work/file state=directory  mode=0700"
192.168.1.21 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0700", 
    "owner": "root", 
    "path": "/work/file", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
}

3、第三个步骤:将脚本权限进行修改(添加执行权限)

批量拷贝shell脚本到各台服务器

ansible 主机组 -m copy -a "src=拷贝文件路径 dest=拷贝目前文件路径 mode=0755

gota@gota-linux61:~$ ansible cluster -m copy -a "src=/home/gota/test.sh dest=/work/ansible mode=0755"
192.168.1.21 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "dcaa896be5c669bbfcc657cc25452d46962aedb6", 
    "dest": "/work/ansible/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "bf53066b75afbc91cd8917d43e10880d", 
    "mode": "0755", 
    "owner": "root", 
    "size": 637, 
    "src": "/root/.ansible/tmp/ansible-tmp-1564985837.03-26369088965074/source", 
    "state": "file", 
    "uid": 0

 4、第四个步骤:运行ansible命令执行脚本

gota@gota-linux61:~$ ansible cluster -m shell -a "/work/ansible/test.sh"
192.168.1.21 | CHANGED | rc=0 >>
/work/ansible

disk_size=10%
unhealth

disk_size=9%
health

disk_size=98%
unhealth

192.168.1.20 | CHANGED | rc=0 >>
/work/ansible

disk_size=5%
health

disk_size=1%
health

disk_size=96%
unhealth

script – Runs a local script on a remote node after transferring it

script模块 跟上面类似  执行脚本只要两步:

1.第一个步骤:编写一个脚本

2.第二个步骤:运行ansible命令执行脚本

gota@gota-linux61:~$ ansible cluster -m script -a "/home/gota/test.sh"
192.168.1.21 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.1.21 closed.
", 
    "stderr_lines": [
        "Shared connection to 192.168.1.21 closed."
    ], 
    "stdout": "/root/.ansible/tmp/ansible-tmp-1564988286.84-276931002259842

disk_size=10%
unhealth

disk_size=9%
health

disk_size=98%
unhealth
", 
    "stdout_lines": [
        "/root/.ansible/tmp/ansible-tmp-1564988286.84-276931002259842", 
        "", 
        "disk_size=10%", 
        "unhealth", 
        "", 
        "disk_size=9%", 
        "health", 
        "", 
        "disk_size=98%", 
        "unhealth"
    ]
}
原文地址:https://www.cnblogs.com/linux985/p/11302136.html