ansible:常用模块介绍
Posted 吴焕
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible:常用模块介绍相关的知识,希望对你有一定的参考价值。
1. command模块:默认模块,执行日常的一些命令,但是不支持通配符,管道,重定向
chdir #切换目录
creates #条件判断,如果该文件没有则执行
removes #如果文件有则执行
[root@master01 ~]# ansible all -a \'chdir=/mnt ls -al\' 192.168.3.100 | CHANGED | rc=0 >> 总用量 0 drwxr-xr-x. 4 root root 29 8月 11 11:57 . dr-xr-xr-x. 20 root root 281 8月 19 01:39 .. drwxr-xr-x. 2 root root 6 8月 10 18:57 hgfs drwxrwxrwx 6 nobody nobody 61 8月 18 22:37 nfs 192.168.3.101 | CHANGED | rc=0 >> 总用量 0 drwxr-xr-x. 4 root root 29 8月 11 20:04 . dr-xr-xr-x. 18 root root 258 8月 10 21:46 .. drwxr-xr-x. 2 root root 6 8月 11 03:36 hgfs drwxrwxrwx 6 nobody nobody 61 8月 18 22:37 nfs 192.168.3.102 | CHANGED | rc=0 >> 总用量 0 drwxr-xr-x. 5 root root 58 8月 20 15:11 . dr-xr-xr-x. 18 root root 258 8月 19 01:46 .. drwxr-xr-x. 2 root root 6 8月 11 01:50 hgfs drwxrwxrwx 6 nobody nobody 61 8月 18 22:37 nfs drwxr-xr-x 3 root root 17 8月 20 15:18 volumes-back-20210820
[root@master01 ~]# ansible all -a \'chdir=/mnt creates=/mnt/volumes-back-20210820 ls -al\' 192.168.3.101 | CHANGED | rc=0 >> 总用量 0 drwxr-xr-x. 4 root root 29 8月 11 20:04 . dr-xr-xr-x. 18 root root 258 8月 10 21:46 .. drwxr-xr-x. 2 root root 6 8月 11 03:36 hgfs drwxrwxrwx 6 nobody nobody 61 8月 18 22:37 nfs 192.168.3.100 | CHANGED | rc=0 >> 总用量 0 drwxr-xr-x. 4 root root 29 8月 11 11:57 . dr-xr-xr-x. 20 root root 281 8月 19 01:39 .. drwxr-xr-x. 2 root root 6 8月 10 18:57 hgfs drwxrwxrwx 6 nobody nobody 61 8月 18 22:37 nfs 192.168.3.102 | SUCCESS | rc=0 >> skipped, since /mnt/volumes-back-20210820 exists
2. shell模块,由于shell模块支持通配符,重定向,管道等,可以设置为默认模块,编辑:/etc/ansible/ansible.cfg
114 module_name = shell #这样我们的默认模块就是shell了,使用时就不用指定-m了
shell的用法基本与command一致:
[root@master01 ~]# ansible all -a \'useradd test01\' 192.168.3.100 | FAILED | rc=9 >> useradd:用户“test01”已存在non-zero return code 192.168.3.101 | CHANGED | rc=0 >> 192.168.3.102 | CHANGED | rc=0 >> [root@master01 ~]# ansible all -a \'ls -l /etc/shadow\' 192.168.3.100 | CHANGED | rc=0 >> ---------- 1 root root 928 8月 14 15:51 /etc/shadow 192.168.3.101 | CHANGED | rc=0 >> ---------- 1 root root 899 8月 22 23:45 /etc/shadow 192.168.3.102 | CHANGED | rc=0 >> ---------- 1 root root 899 8月 22 23:45 /etc/shadow [root@master01 ~]# ansible all -a \'echo centos|passwd --stdin test01\' #支持管道 192.168.3.100 | CHANGED | rc=0 >> 更改用户 test01 的密码 。 passwd:所有的身份验证令牌已经成功更新。 192.168.3.101 | CHANGED | rc=0 >> 更改用户 test01 的密码 。 passwd:所有的身份验证令牌已经成功更新。 192.168.3.102 | CHANGED | rc=0 >> 更改用户 test01 的密码 。 passwd:所有的身份验证令牌已经成功更新。
shell模块可以创建文件夹,文件,也可以同时修改文件属性,但是命令需要一条一条执行,多条命令放在一起,只会执行第1条命令:
[root@master01 ~]# ansible all -a \'chdir=/data touch f2.txt chown test01 f2.txt chmon 700 f2.txt\' [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.3.101 | CHANGED | rc=0 >> 192.168.3.100 | CHANGED | rc=0 >> 192.168.3.102 | CHANGED | rc=0 >> [root@master01 ~]# [root@master01 ~]# [root@master01 ~]# ansible all -a \'ls -l /data/f2.txt\' 192.168.3.100 | CHANGED | rc=0 >> -rw-r--r-- 1 root root 0 8月 22 23:54 /data/f2.txt 192.168.3.101 | CHANGED | rc=0 >> -rw-r--r-- 1 root root 0 8月 22 23:54 /data/f2.txt 192.168.3.102 | CHANGED | rc=0 >> -rw-r--r-- 1 root root 0 8月 22 23:54 /data/f2.txt # 文件属性并没有改变
[root@master01 ~]# ansible all -a \'chmod 700 /data/f2.txt\' [WARNING]: Consider using the file module with mode rather than running \'chmod\'. 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.3.100 | CHANGED | rc=0 >> 192.168.3.101 | CHANGED | rc=0 >> 192.168.3.102 | CHANGED | rc=0 >> [root@master01 ~]# ansible all -a \'ls -l /data/f2.txt\' 192.168.3.100 | CHANGED | rc=0 >> -rwx------ 1 test01 root 0 8月 22 23:54 /data/f2.txt 192.168.3.101 | CHANGED | rc=0 >> -rwx------ 1 test01 root 0 8月 22 23:54 /data/f2.txt 192.168.3.102 | CHANGED | rc=0 >> -rwx------ 1 test01 root 0 8月 22 23:54 /data/f2.txt
3. copy模块;用于拷贝ansible主机文件到被管理端;
group #拷贝后的文件指定组
mode #拷贝后的文件指定权限
owner #指定拥有者
src #源文件,即ansible主机文件
dest #目标主机路径 如果src是一个目录,则dest必须是一个目录
用法:
[root@master01 ~]# ansible all -m copy -a \'src=/etc/redhat-release dest=/data/ owner=test01 group=test01 mode=770\' 192.168.3.101 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7", "dest": "/data/redhat-release", "gid": 1000, "group": "test01", "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3", "mode": "0770", "owner": "test01", "size": 30, "src": "/root/.ansible/tmp/ansible-tmp-1629648215.7165456-2370658-213108327814096/source", "state": "file", "uid": 1000 } 192.168.3.100 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7", "dest": "/data/redhat-release", "gid": 1001, "group": "test01", "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3", "mode": "0770", "owner": "test01", "size": 30, "src": "/root/.ansible/tmp/ansible-tmp-1629648215.6977394-2370656-267621287342767/source", "state": "file", "uid": 1001 } 192.168.3.102 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7", "dest": "/data/redhat-release", "gid": 1000, "group": "test01", "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3", "mode": "0770", "owner": "test01", "size": 30, "src": "/root/.ansible/tmp/ansible-tmp-1629648215.8831189-2370660-48157497330459/source", "state": "file", "uid": 1000 } [root@master01 ~]# [root@master01 ~]# ansible all -a \'ls -l /data/redhat-release\' 192.168.3.100 | CHANGED | rc=0 >> -rwxrwx--- 1 test01 test01 30 8月 23 00:03 /data/redhat-release 192.168.3.101 | CHANGED | rc=0 >> -rwxrwx--- 1 test01 test01 30 8月 23 00:03 /data/redhat-release 192.168.3.102 | CHANGED | rc=0 >> -rwxrwx--- 1 test01 test01 30 8月 23 00:03 /data/redhat-release
4. fetch模块,与copy相反,将被管理端主机文件拷贝到ansible管理主机上。
dest #指定ansible-server的文件路径,为文件夹
src #指定源文件,即远程主机的文件
[root@master01 ~]# ansible all -m fetch -a \'src=/etc/redhat-release dest=/data/os\' #/data/os文件夹没有则会创建 192.168.3.100 | CHANGED => { "changed": true, "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7", "dest": "/data/os/192.168.3.100/etc/redhat-release", "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3", "remote_checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7", "remote_md5sum": null } 192.168.3.101 | CHANGED => { "changed": true, "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7", "dest": "/data/os/192.168.3.101/etc/redhat-release", "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3", "remote_checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7", "remote_md5sum": null } 192.168.3.102 | CHANGED => { "changed": true, "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7", "dest": "/data/os/192.168.3.102/etc/redhat-release", "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3", "remote_checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7", "remote_md5sum": null } [root@master01 ~]# tree /data/os/ /data/os/ ├── 192.168.3.100 │ └── etc │ └── redhat-release ├── 192.168.3.101 │ └── etc │ └── redhat-release └── 192.168.3.102 └── etc └── redhat-release 6 directories, 3 files
5. unarchive模块,用于将ansible-server主机的打包文件传输到远程主机并完成解压;
copy #默认为yes,即从server主机拷贝过去,如果为no,则为远程主机的文件进行解压
creates #如果后面跟的文件已经存在了,就不执行
dest #远程主机的解压路径
group #解压后指定组
mode #解压后指定权限
owner #解压后指定用户
src #源文件
[root@master01 ~]# tar -zcvf /data/etc.tar.gz /etc/sysconfig/ #产生压缩文件 [root@master01 ~]# ansible all -m unarchive -a \'src=/data/etc.tar.gz dest=/data/ owner=test01 mode=700 copy=yes\' [root@master01 ~]# ansible all -a \'ls -l /data/etc\' 192.168.3.100 | CHANGED | rc=0 >> 总用量 4 drwx------ 5 test01 root 4096 8月 11 11:41 sysconfig 192.168.3.101 | CHANGED | rc=0 >> 总用量 4 drwx------ 5 test01 root 4096 8月 11 11:41 sysconfig 192.168.3.102 | CHANGED | rc=0 >> 总用量 4 drwx------ 5 test01 root 4096 8月 11 11:41 sysconfig
6. archive模块
dest #目标文件
format #压缩格式
group
mode
owner
path #远程主机的绝对地址
实例如下:将 /etc/yum.repos.d/文件全部打包发回server主机:
[root@master01 ~]# ansible all -m archive -a \'path=/etc/yum.repos.d/ dest=/data/yum.tar.gz format=gz\' [root@master01 ~]# ansible all -a \'ls -l /data\' |grep yum -rw-r--r-- 1 root root 2336 8月 23 00:25 yum.tar.gz -rw-r--r-- 1 root root 2337 8月 23 00:25 yum.tar.gz -rw-r--r-- 1 root root 1934 8月 23 00:25 yum.tar.gz
然后发回server主机:
[root@master01 ~]# ansible all -m fetch -a \'src=/data/yum.tar.gz dest=/data/yum\' 192.168.3.100 | CHANGED => { "changed": true, "checksum": "c6a133568eb541b62ba1ae17e705d34c9587869d", "dest": "/data/yum/192.168.3.100/data/yum.tar.gz", "md5sum": "65fd881d8f3072ecd64aa0e8c8b52cb9", "remote_checksum": "c6a133568eb541b62ba1ae17e705d34c9587869d", "remote_md5sum": null } 192.168.3.101 | CHANGED => { "changed": true, "checksum": "9662f9158cf80589282e75cc4ec86da760ecd189", "dest": "/data/yum/192.168.3.101/data/yum.tar.gz", "md5sum": "1c903d2142b493cd595147f3fa0748cd", "remote_checksum": "9662f9158cf80589282e75cc4ec86da760ecd189", "remote_md5sum": null } 192.168.3.102 | CHANGED => { "changed": true, "checksum": "5bf5d482d23c92ee38c1d3fe048aed664e18feb9", "dest": "/data/yum/192.168.3.102/data/yum.tar.gz", "md5sum": "4a9c7f3b5e08371267e9eb499d4f92e1", "remote_checksum": "5bf5d482d23c92ee38c1d3fe048aed664e18feb9", "remote_md5sum": null } [root@master01 ~]# tree /data/yum/ /data/yum/ ├── 192.168.3.100 │ └── data │ └── yum.tar.gz ├── 192.168.3.101 │ └── data │ └── yum.tar.gz └── 192.168.3.102 └── data └── yum.tar.gz 6 directories, 3 files
7. Script脚本模块:执行脚本
chdir #在运行脚本之前cd到远程主机的一个目录下
creates #远程主机上的文件存在时,则脚本不运行
如,copy一个server的脚本到远程主机并执行;
1 [root@master01 data]# cat ./test.sh 2 #!/bin/bash 3 echo "welcome to my $HOSTNAME" 4 然后拷贝到远程主机下 5 [root@master01 data]# ansible all -m copy -a \'src=/data/test.sh dest=/data/test-1.sh mode=755\' 6 192.168.3.101 | CHANGED => { 7 "ansible_facts": { 8 "discovered_interpreter_python": "/usr/libexec/platform-python" 9 }, 10 "changed": true, 11 "checksum": "5cf8e18b77ffc1800c384568ac01edcf0b331726", 12 "dest": "/data/test-1.sh", 13 "gid": 0, 14 "group": "root", ansible常用模块介绍Ansible playbooks入门和编写规范Ansible Playbooks常用模块介绍常用模块集合