[root@localhost ansible]# cat hosts |grep -v ^#|grep -v ^$ [webservers] 192.168.1.223 192.168.1.221
我们使用ansible web -m ping
命令来进行主机连通性测试,效果如下:
[root@localhost ansible]# ansible webservers -m ping 192.168.1.221 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.1.223 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
这样就说明我们的主机是连通状态的。接下来的操作才可以正常进行。
[root@localhost ansible]# ansible webservers -m command -a ‘ss -ntl‘ 192.168.1.221 | CHANGED | rc=0 >> State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* 192.168.1.223 | CHANGED | rc=0 >> State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 80 :::3306 :::*
命令模块接受命令名称,后面是空格分隔的列表参数。给定的命令将在所有选定的节点上执行。它不会通过shell进行处理,比如$HOME和操作如"<",">","|",";","&" 工作(需要使用(shell)模块实现这些功能)。注意,该命令不支持| 管道命令
。
下面来看一看该模块下常用的几个命令:
chdir # 在执行命令之前,先切换到该目录 executable # 切换shell来执行命令,需要使用命令的绝对路径 free_form # 要执行的Linux指令,一般使用Ansible的-a参数代替。 creates # 一个文件名,当这个文件存在,则该命令不执行,可以 用来做判断 removes # 一个文件名,这个文件不存在,则该命令不执行
下面我们来看看这些命令的执行效果:
[root@localhost ansible]# ansible webservers -m command -a ‘chdir=/root/ ls‘ #先切换到/data/ 目录,再执行“ls”命令 192.168.1.221 | CHANGED | rc=0 >> anaconda-ks.cfg 192.168.1.223 | CHANGED | rc=0 >> 10.sql 1.sql 4.sql a_mysql0_2020-01-16.sql02.gz a_mysql_2020-01-16.sql.gz anaconda-ks.cfg backup mingongge_bak.sql mysql-community-release-el7-5.noarch.rpm percona-xtrabackup-2.4.7-Linux-x86_64 percona-xtrabackup-2.4.7-Linux-x86_64.tar.gz
[root@localhost ~]# ansible webserver -m command -a ‘creates=/root/aaa.jpg ls‘ #如果/root/aaa.jpg存在,则不执行“ls”命令 [WARNING]: Could not match supplied host pattern, ignoring: webserver [WARNING]: No hosts matched, nothing to do
[root@localhost ~]# ansible webservers -m command -a ‘removes=/root/aaa.jpg cat /root/a‘ #如果/data/aaa.jpg存在,则执行“cat /data/a”命令 192.168.1.221 | CHANGED | rc=0 >> 192.168.1.223 | CHANGED | rc=0 >> [root@localhost ~]#
shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等。
这个模块用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等。
其相关选项如下:
src #被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于"rsync" content #用于替换"src",可以直接指定文件的值 dest #必选项,将源文件复制到的远程主机的绝对路径 backup #当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息 directory_mode #递归设定目录的权限,默认为系统默认权限 force #当目标主机包含该文件,但内容不同时,设为"yes",表示强制覆盖;设为"no",表示目标主机的目标位置不存在该文件才复制。默认为"yes" others #所有的 file 模块中的选项可以在这里使用
用法举例如下:
① 复制文件:
[root@localhost ~]# ansible webservers -m copy -a ‘src=/etc/fstab dest=/root/fstab‘ 192.168.1.221 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "d265038d261fbc05a186fb5b51a6f88104bf5a4e", "dest": "/root/fstab", "gid": 0, "group": "root", "md5sum": "f814419cfabc31ec2c045abfd74bf36a", "mode": "0644", "owner": "root", "size": 465, "src": "/root/.ansible/tmp/ansible-tmp-1579228850.76-172106291812409/source", "state": "file", "uid": 0 } 192.168.1.223 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "d265038d261fbc05a186fb5b51a6f88104bf5a4e", "dest": "/root/fstab", "gid": 0, "group": "root", "md5sum": "f814419cfabc31ec2c045abfd74bf36a", "mode": "0644", "owner": "root", "size": 465, "src": "/root/.ansible/tmp/ansible-tmp-1579228850.73-239212718197228/source", "state": "file", "uid": 0 }
② 给定内容生成文件,并制定权限
[root@localhost ~]# ansible webservers -m copy -a ‘content="I am keer\n" dest=/root/ljj.txt mode=666‘ 192.168.1.221 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "0421570938940ea784f9d8598dab87f07685b968", "dest": "/root/ljj.txt", "gid": 0, "group": "root", "md5sum": "497fa8386590a5fc89090725b07f175c", "mode": "0666", "owner": "root", "size": 10, "src": "/root/.ansible/tmp/ansible-tmp-1579228935.37-220505593867428/source", "state": "file", "uid": 0 } 192.168.1.223 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "0421570938940ea784f9d8598dab87f07685b968", "dest": "/root/ljj.txt", "gid": 0, "group": "root", "md5sum": "497fa8386590a5fc89090725b07f175c", "mode": "0666", "owner": "root", "size": 10, "src": "/root/.ansible/tmp/ansible-tmp-1579228935.37-278506941965905/source", "state": "file", "uid": 0 }
我们现在可以去查看一下我们生成的文件及其权限:
[root@localhost ~]# ansible webservers -m shell -a ‘ls -l /root/ljj.txt‘ 192.168.1.221 | CHANGED | rc=0 >> -rw-rw-rw- 1 root root 10 1月 17 10:42 /root/ljj.txt 192.168.1.223 | CHANGED | rc=0 >> -rw-rw-rw- 1 root root 10 1月 17 10:42 /root/ljj.txt
可以看出我们的ljj.txt文件已经生成,并且权限为666。
③ 关于覆盖
我们把文件的内容修改一下,然后选择覆盖备份:
[root@localhost ~]# ansible webservers -m copy -a ‘content="I am keerya\n" backup=yes dest=/root/ljj.txt mode=666‘ 192.168.1.221 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "backup_file": "/root/ljj.txt.2031.2020-01-17@10:45:26~", "changed": true, "checksum": "064a68908ab9971ee85dbc08ea038387598e3778", "dest": "/root/ljj.txt", "gid": 0, "group": "root", "md5sum": "8ca7c11385856155af52e560f608891c", "mode": "0666", "owner": "root", "size": 12, "src": "/root/.ansible/tmp/ansible-tmp-1579229124.15-218967695281776/source", "state": "file", "uid": 0 } 192.168.1.223 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "backup_file": "/root/ljj.txt.51754.2020-01-17@10:45:26~", "changed": true, "checksum": "064a68908ab9971ee85dbc08ea038387598e3778", "dest": "/root/ljj.txt", "gid": 0, "group": "root", "md5sum": "8ca7c11385856155af52e560f608891c", "mode": "0666", "owner": "root", "size": 12, "src": "/root/.ansible/tmp/ansible-tmp-1579229124.14-266503506198407/source", "state": "file", "uid": 0 }
现在我们可以去查看一下:
[root@localhost ~]# ansible webservers -m shell -a ‘ls -l /root/ljj.*‘
192.168.1.221 | CHANGED | rc=0 >>
-rw-rw-rw- 1 root root 12 1月 17 10:45 /root/ljj.txt
-rw-rw-rw- 1 root root 10 1月 17 10:42 /root/ljj.txt.2031.2020-01-17@10:45:26~
192.168.1.223 | CHANGED | rc=0 >>
-rw-rw-rw- 1 root root 12 1月 17 10:45 /root/ljj.txt
-rw-rw-rw- 1 root root 10 1月 17 10:42 /root/ljj.txt.51754.2020-01-17@10:45:26~
可以看出,我们的源文件已经被备份,我们还可以查看一下ljj.txt文件的内容:
[root@localhost ~]# ansible webservers -m shell -a ‘cat /root/ljj.txt‘ 192.168.1.221 | CHANGED | rc=0 >> I am keerya 192.168.1.223 | CHANGED | rc=0 >> I am keerya
证明,这正是我们新导入的文件的内容。
该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
下面是一些常见的命令:
force #需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no group #定义文件/目录的属组。后面可以加上mode:定义文件/目录的权限 owner #定义文件/目录的属主。后面必须跟上path:定义文件/目录的路径 recurse #递归设置文件的属性,只对目录有效,后面跟上src:被链接的源文件路径,只应用于state=link的情况 dest #被链接到的路径,只应用于state=link的情况 state #状态,有以下选项: directory:如果目录不存在,就创建目录 file:即使文件不存在,也不会被创建 link:创建软链接 hard:创建硬链接 touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间 absent:删除目录、文件或者取消链接文件
① 创建目录:
[root@localhost ~]# ansible webservers -m file -a ‘path=/data/app state=directory‘ 192.168.1.221 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/data/app", "size": 6, "state": "directory", "uid": 0 } 192.168.1.223 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/data/app", "size": 6, "state": "directory", "uid": 0 }
[root@localhost ~]# ansible webservers -m shell -a ‘ls -l /data‘ 192.168.1.221 | CHANGED | rc=0 >> 总用量 0 drwxr-xr-x 2 root root 6 1月 17 10:50 app drwxr-xr-x 3 root root 18 1月 14 10:05 k8s 192.168.1.223 | CHANGED | rc=0 >> 总用量 0 drwxr-xr-x 2 root root 6 1月 17 10:50 app
② 创建链接文件
[root@localhost ~]# ansible webservers -m file -a ‘path=/data/bbb.jpg src=/root/aaa.jpg state=link‘ 192.168.1.221 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/data/bbb.jpg", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 13, "src": "/root/aaa.jpg", "state": "link", "uid": 0 } 192.168.1.223 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/data/bbb.jpg", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 13, "src": "/root/aaa.jpg", "state": "link", "uid": 0 }
[root@localhost ~]# ansible webservers -m shell -a ‘ls -l /data‘ 192.168.1.221 | CHANGED | rc=0 >> 总用量 0 drwxr-xr-x 2 root root 6 1月 17 10:50 app lrwxrwxrwx 1 root root 13 1月 17 10:53 bbb.jpg -> /root/aaa.jpg drwxr-xr-x 3 root root 18 1月 14 10:05 k8s 192.168.1.223 | CHANGED | rc=0 >> 总用量 0 drwxr-xr-x 2 root root 6 1月 17 10:50 app lrwxrwxrwx 1 root root 13 1月 17 10:53 bbb.jpg -> /root/aaa.jpg
③ 删除文件
[root@localhost ~]# ansible webservers -m file -a ‘path=/root/a state=absent‘ 192.168.1.221 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "/root/a", "state": "absent" } 192.168.1.223 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "/root/a", "state": "absent" }
[root@localhost ~]# ansible webservers -m shell -a ‘ls /root/a‘ 192.168.1.221 | FAILED | rc=2 >> ls: 无法访问/root/a: 没有那个文件或目录non-zero return code 192.168.1.223 | FAILED | rc=2 >> ls: 无法访问/root/a: 没有那个文件或目录non-zero return code
该模块用于从远程某主机获取(复制)文件到本地。
有两个选项:
dest:用来存放文件的目录
src:在远程拉取的文件,并且必须是一个file,不能是目录
[root@localhost ~]# ansible webservers -m fetch -a ‘src=/data/bbb.jpg dest=/data‘ 192.168.1.223 | CHANGED => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/data/192.168.1.223/data/bbb.jpg", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null } 192.168.1.221 | CHANGED => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/data/192.168.1.221/data/bbb.jpg", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null }
[root@localhost ~]# cd /data/ [root@localhost data]# ls 192.168.1.221 192.168.1.223 [root@localhost data]# ls 192.168.1.223/data/bbb.jpg 192.168.1.223/data/bbb.jpg [root@localhost data]# ls 192.168.1.221/data/bbb.jpg 192.168.1.221/data/bbb.jpg
该模块适用于管理cron
计划任务的。
其使用的语法跟我们的crontab
文件中的语法一致,同时,可以指定以下选项:
day= #日应该运行的工作( 1-31, , /2, ) hour= # 小时 ( 0-23, , /2, ) minute= #分钟( 0-59, , /2, ) month= # 月( 1-12, *, /2, ) weekday= # 周 ( 0-6 for Sunday-Saturday,, ) job= #指明运行的命令是什么 name= #定时任务描述 reboot # 任务在重启时运行,不建议使用,建议使用special_time special_time #特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时) state #指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务 user # 以哪个用户的身份执行
原文:https://www.cnblogs.com/liujunjun/p/12204954.html