ansible入门之高级用法

765 阅读7分钟

这是我参与11月更文挑战的第14天,活动详情链接查看:2021最后一次更文挑战

1. ansible的常用模块

1) ansible的3个远程模块的区别

  • command : ansible的默认模块,不指定-m参数的时候,使用的就是command模块; 常见的命令都可以使用,但命令的执行不是通过shell来执行的,所以< > | and & z这些操作都不可以,不支持管道,没法批量执行命令

  • shell模块: 使用shell模块的时候默认是通过/bin/sh来执行的,所以在终端输入的各种命令都可以使用

  • scripts模块 使用scripts模块可以在本地写一个脚本,在远程服务器上执行

案例1:使用shell模块的案例
[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts  web-servers -m shell -a "source ~/.bash_profile && df -h|head -n 1"
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
192.168.1.71 | CHANGED | rc=0 >>
文件系统                           容量  已用  可用 已用% 挂载点
192.168.1.163 | CHANGED | rc=0 >>
文件系统                 容量  已用  可用 已用% 挂载点
[root@itlaoxin162 ~]# 

注意: shell也可以把一个脚本copy到远程端然后再执行,但这样的话就需要调用两次ansible,所以script的出现就解决了这个问题;

案例2:使用script 模块

先写一个脚本:

[root@itlaoxin162 ~]# cat !$
cat /etc/ansible/test.sh
#!/bin/bash
date
hostname
echo "大家好,我是互联网老辛,脚本执行成功"
[root@itlaoxin162 ~]# 

执行查看结果:

[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts web-servers -m script -a "/etc/ansible/test.sh" 
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
192.168.1.71 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.1.71 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.1.71 closed."
    ], 
    "stdout": "2021年 04月 21日 星期三 08:59:33 CST\r\nk8s-master\r\n大家好,我是互联网老辛,脚本执行成功\r\n", 
    "stdout_lines": [
        "2021年 04月 21日 星期三 08:59:33 CST", 
        "k8s-master", 
        "大家好,我是互联网老辛,脚本执行成功"
    ]
}
192.168.1.163 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.1.163 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.1.163 closed."
    ], 
    "stdout": "2021年 04月 21日 星期三 08:59:33 CST\r\nitlaoxin-163\r\n大家好,我是互联网老辛,脚本执行成功\r\n", 
    "stdout_lines": [
        "2021年 04月 21日 星期三 08:59:33 CST", 
        "itlaoxin-163", 
        "大家好,我是互联网老辛,脚本执行成功"
    ]
}
[root@itlaoxin162 ~]# 

可以看到已经执行成功

2) copy模块的使用

copy模块:实现主控端向目标主机拷贝文件,类似scp功能

案例1: 把ansible主机的/etc/hosts 拷贝到主机组机器中的/root/下
[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts web-servers -m copy -a "src=/etc/hosts dest=/root owner=root group=root mode=0777"
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
192.168.1.71 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "700a03c76a37e929d448b3be6419f4289d9314e6", 
    "dest": "/root/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "138004edd9d16f2818e20842fc1f273d", 
    "mode": "0777", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 183, 
    "src": "/root/.ansible/tmp/ansible-tmp-1618966980.44-20046-203314294949142/source", 
    "state": "file", 
    "uid": 0
}
192.168.1.163 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "700a03c76a37e929d448b3be6419f4289d9314e6", 
    "dest": "/root/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "138004edd9d16f2818e20842fc1f273d", 
    "mode": "0777", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 183, 
    "src": "/root/.ansible/tmp/ansible-tmp-1618966980.45-20045-254958397204815/source", 
    "state": "file", 
    "uid": 0
}
[root@itlaoxin162 ~]# 

查看是否执行成功:

[root@itlaoxin162 ~]# ansible -m command -a "ls /root/hosts" 'web-servers'
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
192.168.1.71 | CHANGED | rc=0 >>
/root/hosts
192.168.1.163 | CHANGED | rc=0 >>
/root/hosts
[root@itlaoxin162 ~]# 

注意: command 不能使用ll命令,但可以使用ls -l的命令

[root@itlaoxin162 ~]# ansible -m command -a "ls -l  /root/hosts" 'web-servers'
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
192.168.1.71 | CHANGED | rc=0 >>
-rwxrwxrwx. 1 root root 183 4月  21 09:03 /root/hosts
192.168.1.163 | CHANGED | rc=0 >>
-rwxrwxrwx. 1 root root 183 4月  21 09:03 /root/hosts
[root@itlaoxin162 ~]# 

3. file模块

案例5 给文件设置权限
[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts web-servers -m file -a "path=/root/hosts mode=0755"
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
192.168.1.71 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/root/hosts", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 183, 
    "state": "file", 
    "uid": 0
}
192.168.1.163 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/root/hosts", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 183, 
    "state": "file", 
    "uid": 0
}
[root@itlaoxin162 ~]# 

查看权限:

[root@itlaoxin162 ~]# ansible -m command -a "ls -l  /root/hosts" 'web-servers'
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
192.168.1.71 | CHANGED | rc=0 >>
-rwxr-xr-x. 1 root root 183 4月  21 09:03 /root/hosts
192.168.1.163 | CHANGED | rc=0 >>
-rwxr-xr-x. 1 root root 183 4月  21 09:03 /root/hosts
[root@itlaoxin162 ~]# 

4. stat模块获取远程文件信息

案例6 获取文件信息
[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts web-servers -m stat -a "path=/root/hosts"
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
192.168.1.71 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "stat": {
        "atime": 1618966982.400622, 
        "attr_flags": "", 
        "attributes": [], 
        "block_size": 4096, 
        "blocks": 8, 
        "charset": "us-ascii", 
        "checksum": "700a03c76a37e929d448b3be6419f4289d9314e6", 
        "ctime": 1618967480.9315438, 
        "dev": 64768, 
        "device_type": 0, 
        "executable": true, 
        "exists": true, 
        "gid": 0, 
        "gr_name": "root", 
        "inode": 78337, 
        "isblk": false, 
        "ischr": false, 
        "isdir": false, 
        "isfifo": false, 
        "isgid": false, 
        "islnk": false, 
        "isreg": true, 
        "issock": false, 
        "isuid": false, 
        "mimetype": "text/plain", 
        "mode": "0755", 
        "mtime": 1618966981.7806218, 
        "nlink": 1, 
        "path": "/root/hosts", 
        "pw_name": "root", 
        "readable": true, 
        "rgrp": true, 
        "roth": true, 
        "rusr": true, 
        "size": 183, 
        "uid": 0, 
        "version": "693378940", 
        "wgrp": false, 
        "woth": false, 
        "writeable": true, 
        "wusr": true, 
        "xgrp": true, 
        "xoth": true, 
        "xusr": true
    }
}
192.168.1.163 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "stat": {
        "atime": 1618966982.6472814, 
        "attr_flags": "", 
        "attributes": [], 
        "block_size": 4096, 
        "blocks": 8, 
        "charset": "us-ascii", 
        "checksum": "700a03c76a37e929d448b3be6419f4289d9314e6", 
        "ctime": 1618967481.0644567, 
        "dev": 64768, 
        "device_type": 0, 
        "executable": true, 
        "exists": true, 
        "gid": 0, 
        "gr_name": "root", 
        "inode": 33662547, 
        "isblk": false, 
        "ischr": false, 
        "isdir": false, 
        "isfifo": false, 
        "isgid": false, 
        "islnk": false, 
        "isreg": true, 
        "issock": false, 
        "isuid": false, 
        "mimetype": "text/plain", 
        "mode": "0755", 
        "mtime": 1618966982.176287, 
        "nlink": 1, 
        "path": "/root/hosts", 
        "pw_name": "root", 
        "readable": true, 
        "rgrp": true, 
        "roth": true, 
        "rusr": true, 
        "size": 183, 
        "uid": 0, 
        "version": "1103139934", 
        "wgrp": false, 
        "woth": false, 
        "writeable": true, 
        "wusr": true, 
        "xgrp": true, 
        "xoth": true, 
        "xusr": true
    }
}


5. get_url 模块

实现远程主机下载指定的url地址,支持sha256sum文件校验

案例7
ansible -i /etc/ansible/hosts web-servers -m get_url -a "url=https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm  dest=/tmp/ mode=0440 force=yes"

注:url=https://xxx 的等号=前后不能有空格 扩展:查看force=yes的作用

6. yum模块

yum模块linux平台软件包管理。 yum模块可以提供的status状态: latest ,present,installed #这三个代表安装;removed, absent #这两个是卸载

案例8 使用yum模块安装httpd

ansible -i /etc/ansible/hosts web-servers -m yum -a "name=httpd state=latest"

7. cron模块远程管理主机crontab配置

案例9: 增加每30分钟执行 echo"我是互联网老辛"

ansible -i /etc/ansible/hosts web-servers -m cron -a "name='list dir' minute='*/30' job='echo 我是互联网老辛”'"

8. service 远程管理主机系统服务模块

service模块常用参数: (1)、name参数:此参数用于指定需要操作的服务名称,比如 nginx,httpd。 (2)、state参数:此参数用于指定服务的状态

比如,我们想要启动远程主机中的httpd,则可以将 state 的值设置为 started; 如果想要停止远程主机中的服务,则可以将 state 的值设置为 stopped。 此参数的可用值有 started、stopped、restarted(重启)、reloaded。

enabled参数:此参数用于指定是否将服务设置为开机 启动项,设置为 yes 表示将对应服务设置为开机启动,设置为 no 表示不会开机启动。

注:想使用service模块启动服务,被启动的服务,必须可以使用service 命令启动或关闭

案例10 使用service模块重启httpd
[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts web-servers -m service -a "name=httpd state=restarted"

9. user模块 管理远程主机的用户

案例11: 使用user模块创建一个用户itlaoxin
[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts web-servers -m user -a "name=itlaoxin state=present"
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
192.168.1.71 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1002, 
    "home": "/home/itlaoxin", 
    "name": "itlaoxin", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1002
}
192.168.1.163 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1002, 
    "home": "/home/itlaoxin", 
    "name": "itlaoxin", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1002
}
[root@itlaoxin162 ~]# 




## 总结
ansible的模块有很多,以上是一些常见的模块,其他模块需要看官方文档学习