介绍Ansible的一些模块(持续更新)
copy ---从本地或远程计算机复制文件到远程计算机
案例
本地文件复制到远程主机
[root@bogon ansible]# ansible 192.168.10.135 -m copy -a "src=/etc/ansible/test dest=/ansible"
192.168.10.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "c6085ba4f6e3f8a10e134285441ff9d12647da46",
"dest": "/ansible/test",
"gid": 0,
"group": "root",
"md5sum": "22d9ae3f5798594408d8537b8d5ca3cd",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:default_t:s0",
"size": 15,
"src": "/root/.ansible/tmp/ansible-tmp-1646980002.84-1570-59951029648924/source",
"state": "file",
"uid": 0
}
[root@agent ansible]# ls
test
当src为目录且不以/结尾,那么文件本身也被复制到远程主机上。
[root@bogon ansible]# ansible 192.168.10.135 -m copy -a "src=/etc/ansible/test_dir dest=/ansible"
192.168.10.135 | CHANGED => {
"changed": true,
"dest": "/ansible/",
"src": "/etc/ansible/test_dir"
}
[root@agent ansible]# tree
.
├── test
└── test_dir
├── 1.txt
├── 2.txt
└── 3.txt
当src为目录并且以/结尾,那么只会将src目录下的文件复制到dest目录中。
[root@bogon ansible]# ansible 192.168.10.135 -m copy -a "src=/etc/ansible/test_dir/ dest=/ansible"
192.168.10.135 | CHANGED => {
"changed": true,
"dest": "/ansible/",
"src": "/etc/ansible/test_dir/"
}
[root@agent ansible]# tree
.
├── 1.txt
├── 2.txt
├── 3.txt
└── test
当src为一个目录时,dest必须也是一个目录,否则报错。
[root@bogon ansible]# ansible 192.168.10.135 -m copy -a "src=/etc/ansible/test_dir/ dest=/ansible/test"
192.168.10.135 | FAILED! => {
"msg": "Failed to get information on remote file (/ansible/test/1.txt): Not a directory"
}
当dest文件/目录不存在时,远程主机自动创建。
[root@bogon ansible]# ansible 192.168.10.135 -m copy -a "src=/etc/ansible/test dest=/ansible/"
192.168.10.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "c6085ba4f6e3f8a10e134285441ff9d12647da46",
"dest": "/ansible/test",
"gid": 0,
"group": "root",
"md5sum": "22d9ae3f5798594408d8537b8d5ca3cd",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 15,
"src": "/root/.ansible/tmp/ansible-tmp-1646989026.56-2061-185632474691347/source",
"state": "file",
"uid": 0
}
在远程复制文件时,修改文件的权限以及所属信息。
[root@bogon ansible]# ansible 192.168.10.135 -m copy -a "src=/etc/ansible/test_dir/ dest=/ansible/wh owner=wh group=wh mode=777"
192.168.10.135 | CHANGED => {
"changed": true,
"dest": "/ansible/wh/",
"src": "/etc/ansible/test_dir/"
}
[root@agent wh]# ll
total 0
-rwxrwxrwx. 1 wh wh 0 Mar 13 23:16 1.txt
-rwxrwxrwx. 1 wh wh 0 Mar 13 23:16 2.txt
-rwxrwxrwx. 1 wh wh 0 Mar 13 23:16 3.txt
content替代src,在远程主机上创建文件并输入content内容。
[root@bogon ansible]# ansible 192.168.10.135 -m copy -a "content='whwh' dest=/ansible/wh/test_txt"
192.168.10.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "5459263fd657f6fd44579cbe32757af70d43b2fd",
"dest": "/ansible/wh/test_txt",
"gid": 0,
"group": "root",
"md5sum": "f03ca53b0d734cfda168456451080d52",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:default_t:s0",
"size": 4,
"src": "/root/.ansible/tmp/ansible-tmp-1647228225.64-1973-2873745411266/source",
"state": "file",
"uid": 0
}
[root@agent wh]# ls
1.txt 2.txt 3.txt test_txt
[root@agent wh]# cat test_txt
whwh
复制时,备份远程主机文件
[root@bogon ansible]# ansible 192.168.10.135 -m copy -a "src=/etc/ansible/test dest=/ansible/test backup=yes"
192.168.10.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/ansible/test.2744.2022-03-13@23:27:52~",
"changed": true,
"checksum": "2c9f512bcb3b797456ed1255fbbe471d8f1971c8",
"dest": "/ansible/test",
"gid": 0,
"group": "root",
"md5sum": "02cea806a7e4bc4615062420e5e45a60",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 19,
"src": "/root/.ansible/tmp/ansible-tmp-1647228471.72-2042-42449986313640/source",
"state": "file",
"uid": 0
}
[root@agent ansible]# ls
test test.2744.2022-03-13@23:27:52~ test_dir
file--设置远程主机文件属性
案例
修改远程主机文件属性
[root@bogon ansible]# ansible 192.168.10.135 -m file -a "dest=/ansible/wh owner=wh"
192.168.10.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "wh",
"path": "/ansible/wh",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 61,
"state": "directory",
"uid": 1000
}
[root@agent ansible]# ll
total 8
-rw-r--r--. 1 root root 19 Mar 13 23:27 test
-rw-r--r--. 1 root root 15 Mar 11 03:57 test.2744.2022-03-13@23:27:52~
drwxr-xr-x. 2 root root 45 Mar 11 03:58 test_dir
drwxr-xr-x. 2 wh root 61 Mar 13 23:23 wh
state参数
删除文件
[root@bogon ansible]# ansible 192.168.10.135 -m file -a "dest=/ansible/test state=absent"
创建文件
[root@bogon ansible]# ansible 192.168.10.135 -m file -a "dest=/ansible/test state=directory"
查看文件是否存在
[root@bogon ansible]# ansible 192.168.10.135 -m file -a "dest=/ansible/test state=file"
创建软连接
[root@bogon ansible]# ansible 192.168.10.135 -m file -a "src=/ansible/link dest=/ansible/link_links state=link"
其中dest可自动创建文件,如源文件存在,则不可创建
创建硬链接
[root@bogon ansible]# ansible 192.168.10.135 -m file -a "src=/ansible/test dest=/ansible/hard_link state=hard"
创建文件
[root@bogon ansible]# ansible 192.168.10.135 -m file -a "dest=/ansible/test123 state=touch"
yum--安装卸载软件
安装iotop
[root@bogon ansible]# ansible 192.168.10.135 -m yum -a "name=iotop state=installed"
卸载iotop
ansible 192.168.10.135 -m yum -a "name=iotop state=absent"
service---管理服务器的状态
关闭服务
[root@bogon ansible]# ansible 192.168.10.135 -m service -a "name=firewalld state=stopped"
| state | 说明 |
|---|---|
| stopped | 关闭 |
| started | 开启 |
| reloaded | 重载 |
| restarted | 重启 |
| enable | 说明 |
|---|---|
| yes | 开机自启动 |
| no | 开机不自启 |