学海无涯
go,go,go

ansible-常用模块:ping、command、shell、script(一)

测试ping模块,197和196密码一样可以直接2台主机测试通不通
#-k链接指定要输入链接密码,默认key验证
#-m指定使用什么模块
ping模块是使用ssh协议
[root@ansible ~]# ansible 192.168.2.196,192.168.2.197 -m ping -k
SSH password: 
192.168.2.196 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.2.197 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

#3台主机的时候,197和196密码一样,189密码不一样就无法连接,输入的密码是197和196的密码
[root@ansible ~]# ansible 192.168.2.196,192.168.2.197,192.168.2.189 -m ping -k
SSH password: 
192.168.2.189 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}
192.168.2.197 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.2.196 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
#红色的是报错,绿色的就是成功

ping模块依赖ssh服务
[root@lnmp ~]# ansible test -m ping
10.128.30.74 | SUCCESS => {
"changed": false, 
"ping": "pong"
}
检查被管理机器ssh通道是否正常,并且验证ssh是否可以被远程登录
说明:一个简单的测试模块,这个模块总是返回"pong"在成功连接时。这个模块在剧本中是没有意义的。但是能够使用ansible命令验证登录能力和用于python的配置
这个并不是传统的ICMP ping,而是先检查能否通过ssh登录节点,再检查python版本能否满足要求,能满足要求返回pong。这只是一个简单测试模块

当被管理机器的ssh被关闭时会卡住


debug模块,测试是否通信成功
[root@lnmp ~]# ansible test -m debug
10.128.30.74 | SUCCESS => {
"msg": "Hello world!"
}

把Hello world换成自己想要的东西
[root@lnmp ~]# ansible test -m debug -a "msg=test-debug"
10.128.30.74 | SUCCESS => {
"msg": "test-debug"
}
使用msg把Hello world替换成自己想要的字符test-debug
command模块是命令模块可以使用此模块在远端被管理主机执行命令
[root@ansible ~]# ansible-doc -s command
- name: Execute commands on targets
command:
    argv:               # Passes the command as a list rather than a string. Use `argv' to avoid quoting values that would otherwise be interpreted incorrectly (for example "user
                          name"). Only the string or the list form can be provided, not both.  One or the other must be provided.
    chdir:              # Change into this directory before running the command.
    cmd:                # The command to run.
    creates:            # A filename or (since 2.0) glob pattern. If it already exists, this step *won't* be run.
    free_form:          # The command module takes a free form command to run. There is no actual parameter named 'free form'.
    removes:            # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.
    stdin:              # Set the stdin of the command directly to the specified value.
    stdin_add_newline:  # If set to `yes', append a newline to stdin data.
    strip_empty_ends:   # Strip empty lines from the end of stdout/stderr in result.
    warn:               # Enable or disable task warnings.

#creates=file指定文件存在就不执行这个命令,不存在就执行指定的命令
/etc/fstab文件存在不执行命令
[root@ansible ~]# ansible web -m command -a "creates=/etc/fstab ls /root"
192.168.2.196 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists
192.168.2.189 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists

#/etc/asdasjkdh文件不存在就执行ls /root命令
[root@ansible ~]# ansible web -m command -a "creates=/etc/asdasjkdh ls /root"
192.168.2.196 | CHANGED | rc=0 >>
anaconda-ks.cfg
yh.sh
192.168.2.189 | CHANGED | rc=0 >>
anaconda-ks.cfg
yh.sh


#removes=file和creates相反不存在不执行命令,存在才执行命令
#文件不存在就不执行命令
[root@ansible ~]# ansible web -m command -a "removes=/etc/fstabsss ls -l /root"
192.168.2.189 | SUCCESS | rc=0 >>
skipped, since /etc/fstabsss does not exist

192.168.2.196 | SUCCESS | rc=0 >>
skipped, since /etc/fstabsss does not exist
#文件存在就执行命令
[root@ansible ~]# ansible web -m command -a "removes=/etc/fstab ls -l /root"
192.168.2.189 | CHANGED | rc=0 >>
total 8
-rw-------. 1 root root 1463 Feb  4  2015 anaconda-ks.cfg
-rw-r--r--. 1 root root 2576 Feb  4  2015 yh.sh

192.168.2.196 | CHANGED | rc=0 >>
total 8
-rw-------. 1 root root 1463 Feb  4  2015 anaconda-ks.cfg
-rw-r--r--. 1 root root 2576 Feb  4  2015 yh.sh

#chdir=/opt表示切换到/opt目录下执行ls -l命令
[root@ansible ~]# ansible web -m command -a "chdir=/opt ls -l"
192.168.2.196 | CHANGED | rc=0 >>
total 16
drwxr-xr-x 3 root root    30 Jan 16 15:01 module
-rw-r--r-- 1 root root 15137 Jan 17 14:33 zookeeper.out

192.168.2.189 | CHANGED | rc=0 >>
total 0


#所有被管理端添加执行脚本
cat >/opt/test.sh<<'EOF'
#!/bin/bash
hostname
ls -l
pwd
EOF
chmod a+x /opt/test.sh
#管理端执行脚本测试
[root@ansible ~]# ansible all -m command -a "chdir=/opt/ ./test.sh"
192.168.2.189 | CHANGED | rc=0 >>
centos7
total 4
-rwxr-xr-x 1 root root 31 Jan 19 00:33 test.sh
/opt

192.168.2.197 | CHANGED | rc=0 >>
hadoop104
total 4
drwxr-xr-x 3 root root 30 Jan 16 15:01 module
-rwxr-xr-x 1 root root 31 Jan 19 00:33 test.sh
/opt

192.168.2.196 | CHANGED | rc=0 >>
hadoop103
total 20
drwxr-xr-x 3 root root    30 Jan 16 15:01 module
-rwxr-xr-x 1 root root    31 Jan 19 00:33 test.sh
-rw-r--r-- 1 root root 15137 Jan 17 14:33 zookeeper.out
/opt


#使用command模块创建test1用户
[root@ansible ~]# ansible web -a 'useradd test11'
192.168.2.196 | CHANGED | rc=0 >>


192.168.2.189 | CHANGED | rc=0 >>

#使用command模块为test11用户创建密码qwe123
[root@ansible ~]# ansible web -a 'echo qwe123|passwd --stdin test11'
192.168.2.196 | CHANGED | rc=0 >>
qwe123|passwd --stdin test11

192.168.2.189 | CHANGED | rc=0 >>
qwe123|passwd --stdin test11
#执行命令添加密码,执行修改密码因为command的特性修改密码失败,因为command模块对于变量还有特殊符号如<,>,|,&会无法工作,要使用特殊符号和变量就用shell模块
[root@ansible ~]# ansible web -a 'getent shadow test11'
192.168.2.196 | CHANGED | rc=0 >>
test11::18280:0:99999:7:::

192.168.2.189 | CHANGED | rc=0 >>
test11::18280:0:99999:7:::


command创建目录
[root@ansible tmp]# ansible all -a 'mkdir /test -p'
[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 this message.

192.168.2.189 | CHANGED | rc=0 >>


192.168.2.196 | CHANGED | rc=0 >>


192.168.2.197 | CHANGED | rc=0 >>


[root@ansible tmp]# ansible all -a 'ls -ld /test'
192.168.2.196 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Jan 19 14:23 /test

192.168.2.189 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Jan 19 14:23 /test

192.168.2.197 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Jan 19 14:23 /test
shell万能模块
远程查看文件
[root@lnmp ~]# ansible test -m shell -a "cat /tmp/hosts"
10.128.30.74 | SUCCESS | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
lnmp 10.128.30.41
a


shell模块分发文件
[root@lnmp test]# ansible test -m shell -a "rsync -rp /etc/init.d/sshd /tmp"
10.128.30.74 | SUCCESS | rc=0 >>

分发hosts文件
[root@lnmp tmp]# ansible test -m shell -a "cat /tmp/hosts"
10.128.30.74 | SUCCESS | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.128.30.74 lamp

切换到/tmp目录并查看hosts的文件
[root@lnmp tmp]# ansible test -m shell -a "cd /tmp;cat hosts"
10.128.30.74 | SUCCESS | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.128.30.74 lamp


注意分发的文件,管理机分发文件的目录不能和被控机同一个目录负责会报错
[root@lnmp tmp]# ansible test -m shell -a "rsync -rp /tmp/aaa /tmp/"
10.128.30.74 | FAILED | rc=23 >>
rsync: link_stat "/tmp/aaa" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]non-zero return code


把脚本拷贝到被控主机并执行脚本
[root@lnmp scripts]# cat test.sh 
yum install -y iftop

拷贝脚本到被控主机
[root@lnmp scripts]# ansible test -m copy -a "src=/server/scripts/test.sh dest=/server/scripts/"
10.128.30.74 | SUCCESS => {
"changed": true, 
"checksum": "6c9f53799affeab1b43c768a0dca77c4f7be030d", 
"dest": "/server/scripts/test.sh", 
"gid": 0, 
"group": "root", 
"md5sum": "b048e89be9c06260d8e046f90f9b0955", 
"mode": "0644", 
"owner": "root", 
"size": 21, 
"src": "/root/.ansible/tmp/ansible-tmp-1544077747.64-277795547764583/source", 
"state": "file", 
"uid": 0
}

把脚本拷贝到被控主机并执行脚本
[root@lnmp scripts]# cat test.sh 
yum install -y iftop

执行被控机的脚本执行安装
[root@lnmp scripts]# ansible test -m shell -a "/bin/sh /server/scripts/test.sh"
10.128.30.74 | SUCCESS | rc=0 >>
已加载插件:fastestmirror, security
设置安装进程
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
解决依赖关系
--> 执行事务检查
---> Package iftop.x86_64 0:1.0-0.14.pre4.el6 will be 安装
--> 完成依赖关系计算

依赖关系解决

================================================================================
软件包 架构 版本 仓库 大小
================================================================================
正在安装:
iftop x86_64 1.0-0.14.pre4.el6 epel 49 k

事务概要
================================================================================
Install 1 Package(s)

总下载量:49 k
Installed size: 89 k
下载软件包:
运行 rpm_check_debug 
执行事务测试
事务测试成功
执行事务
正在安装 : iftop-1.0-0.14.pre4.el6.x86_64 1/1 
Verifying : iftop-1.0-0.14.pre4.el6.x86_64 1/1 

已安装:
iftop.x86_64 0:1.0-0.14.pre4.el6 

完毕!

被控机查看命令是否安装成功
[root@lamp scripts]# which iftop
/usr/sbin/iftop
成功安装命令

ansible模块---shell(万能模块)
ansible oldboy -m shell -a "/bin/sh /server/scripts/yum.sh"
ansible oldboy -m shell -a "cat /etc/hosts >/tmp/oldboy39.txt"
ansible test -m shell -a "你要执行的任何命令"
说明:script模块与she!l模块的区别·
shell:需要将脚本文件复制到远程服务器,然后执行远程服务器的脚本
script:不需要将脚本文件复制到远程服务器,实质是将脚本执行过程在远程服务器上进行执行·
#在管理端添加脚本
cat >/tmp/l.sh<<'EOF'
#!/bin/bash
hostname
date
df -h
EOF
#配置权限
chmod a+x /tmp/l.sh


[root@ansible ~]# ansible web -m script -a '/tmp/l.sh'
192.168.2.189 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.2.189 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.2.189 closed."
    ], 
    "stdout": "centos7\r\nSun Jan 19 10:45:07 CST 2020\r\nFilesystem      Size  Used Avail Use% Mounted on\r\n/dev/sda3        97G  1.7G   95G   2% /\r\ndevtmpfs        901M     0  901M   0% /dev\r\ntmpfs           911M     0  911M   0% /dev/shm\r\ntmpfs           911M   18M  894M   2% /run\r\ntmpfs           911M     0  911M   0% /sys/fs/cgroup\r\n/dev/sda1       197M  112M   85M  57% /boot\r\ntmpfs           183M     0  183M   0% /run/user/0\r\n", 
    "stdout_lines": [
        "centos7", 
        "Sun Jan 19 10:45:07 CST 2020", 
        "Filesystem      Size  Used Avail Use% Mounted on", 
        "/dev/sda3        97G  1.7G   95G   2% /", 
        "devtmpfs        901M     0  901M   0% /dev", 
        "tmpfs           911M     0  911M   0% /dev/shm", 
        "tmpfs           911M   18M  894M   2% /run", 
        "tmpfs           911M     0  911M   0% /sys/fs/cgroup", 
        "/dev/sda1       197M  112M   85M  57% /boot", 
        "tmpfs           183M     0  183M   0% /run/user/0"
    ]
}
192.168.2.196 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.2.196 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.2.196 closed."
    ], 
    "stdout": "hadoop103\r\nSun Jan 19 10:45:07 CST 2020\r\nFilesystem      Size  Used Avail Use% Mounted on\r\n/dev/sda3        97G  2.0G   95G   3% /\r\ndevtmpfs        901M     0  901M   0% /dev\r\ntmpfs           911M     0  911M   0% /dev/shm\r\ntmpfs           911M   26M  886M   3% /run\r\ntmpfs           911M     0  911M   0% /sys/fs/cgroup\r\n/dev/sda1       197M  112M   85M  57% /boot\r\ntmpfs           183M     0  183M   0% /run/user/0\r\n", 
    "stdout_lines": [
        "hadoop103", 
        "Sun Jan 19 10:45:07 CST 2020", 
        "Filesystem      Size  Used Avail Use% Mounted on", 
        "/dev/sda3        97G  2.0G   95G   3% /", 
        "devtmpfs        901M     0  901M   0% /dev", 
        "tmpfs           911M     0  911M   0% /dev/shm", 
        "tmpfs           911M   26M  886M   3% /run", 
        "tmpfs           911M     0  911M   0% /sys/fs/cgroup", 
        "/dev/sda1       197M  112M   85M  57% /boot", 
        "tmpfs           183M     0  183M   0% /run/user/0"
    ]
}

shell和command模块执行脚本的话脚本都是在远端主机有才能执行而script执行的话只需要管理端有脚本就复制到远端被管理机执行然后删除
当特别多主机执行script模块判断成不成功的话可以查看日志确定成不成功
赞(0) 打赏
未经允许不得转载:YYQ运维技术博客_运维的工作学习之路 » ansible-常用模块:ping、command、shell、script(一)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

运维devops

联系我们关于本博客

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏