command模块
#查看所有主机日期
ansible all -m command -a 'date'
#查看web组中主机root目录
ansible web -a 'ls /root'
[root@ansible ~]# ansible all -m command -a 'date'
192.168.88.20 | CHANGED | rc=0 >>
2021年 10月 08日 星期五 09:16:17 CST
192.168.88.30 | CHANGED | rc=0 >>
2021年 10月 08日 星期五 09:16:17 CST
[root@ansible ~]# ansible web -m command -a 'ls /root'
192.168.88.20 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
公共
模板
视频
图片
文档
下载
音乐
桌面
cron模块
#web组中主机每分钟输出MQ,任务名为test
ansible web -m cron -a 'minute="*/1" job="/usr/bin/echo MQ" name="test"'
#查看计划性任务
ansible web -a 'crontab -l'
#移除计划性任务(absent)
ansible web -m cron -a 'name="test" state=absent'
[root@ansible ~]# ansible web -m cron -a 'minute="*/1" job="/usr/bin/echo MQ" name="test"'
192.168.88.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"test"
]
}
[root@ansible ~]# ansible web -a 'crontab -l'
192.168.88.20 | CHANGED | rc=0 >>
#Ansible: test
*/1 * * * * /usr/bin/echo MQ
[root@ansible ~]# ansible web -m cron -a 'name="test" state=absent'
192.168.88.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
[root@ansible ~]# ansible web -a 'crontab -l'
192.168.88.20 | CHANGED | rc=0 >>
user模块
#所有主机创建用户
ansible all -m user -a 'name=zhangsan'
#查看用户账户文件最后一个
ansible all -m 'command' -a 'tail -1 /etc/passwd'
#移除用户
ansible all -m user -a 'name="zhangsan" state=absent'
[root@ansible ~]# ansible all -m user -a 'name=zhangsan'
192.168.88.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/zhangsan",
"name": "zhangsan",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
192.168.88.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/zhangsan",
"name": "zhangsan",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
[root@ansible ~]# ansible all -m 'command' -a 'tail -1 /etc/passwd'
192.168.88.20 | CHANGED | rc=0 >>
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
192.168.88.30 | CHANGED | rc=0 >>
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
[root@ansible ~]# ansible all -m user -a 'name="zhangsan" state=absent'
192.168.88.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "zhangsan",
"remove": false,
"state": "absent"
}
192.168.88.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "zhangsan",
"remove": false,
"state": "absent"
}
[root@ansible ~]# ansible all -m 'command' -a 'tail -1 /etc/passwd'
192.168.88.20 | CHANGED | rc=0 >>
root2:x:1000:1000:root2:/home/root2:/bin/bash
192.168.88.30 | CHANGED | rc=0 >>
root3:x:1000:1000:root3:/home/root3:/bin/bash
group模块
#创建mysql组,并加入系统组
ansible mysql -m group -a 'name=mysql system=yes'
#查看组账户信息(配置文件最后一个)
ansible mysql -a 'tail -1 /etc/group'
#创建用户并加入组
ansible mysql -m user -a 'name=zhangsan uid=1234 group=mysql system=yes'
#查看用户id和组id信息
ansible mysql -a 'id zhangsan'
[root@ansible ~]# ansible mysql -m group -a 'name=mysql system=yes'
192.168.88.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 985,
"name": "mysql",
"state": "present",
"system": true
}
[root@ansible ~]# ansible mysql -a 'tail -1 /etc/group'
192.168.88.30 | CHANGED | rc=0 >>
mysql:x:985:
[root@ansible ~]# ansible mysql -m user -a 'name=zhangsan uid=1234 group=mysql system=yes'
192.168.88.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 985,
"home": "/home/zhangsan",
"name": "zhangsan",
"shell": "/bin/bash",
"state": "present",
"stderr": "useradd:警告:此主目录已经存在。\n不从 skel 目录里向其中复制任何文件。\n",
"stderr_lines": [
"useradd:警告:此主目录已经存在。",
"不从 skel 目录里向其中复制任何文件。"
],
"system": true,
"uid": 1234
}
[root@ansible ~]# ansible mysql -a 'id zhangsan'
192.168.88.30 | CHANGED | rc=0 >>
uid=1234(zhangsan) gid=985(mysql) 组=985(mysql)
copy模块
#把控制端源文件复制到mysql组中主机并更名
ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back'
#查看文件
ansible mysql -a 'cat /opt/fstab.back'
#复制自定义内容并查看
ansible mysql -m copy -a 'content="MQ" dest=/opt/test.txt'
ansible mysql -a 'cat /opt/test.txt'
[root@ansible ~]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back'
192.168.88.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "d980305132b3ff06623c5f6b90f8e5400f13205f",
"dest": "/opt/fstab.back",
"gid": 0,
"group": "root",
"md5sum": "2bbc6f8adc83a77d8fd406b462e7a5da",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 501,
"src": "/root/.ansible/tmp/ansible-tmp-1633657422.53-45069-216167797362841/source",
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible mysql -a 'cat /opt/fstab.back'
192.168.88.30 | CHANGED | rc=0 >>
#
# /etc/fstab
# Created by anaconda on Thu Sep 9 17:57:44 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=1cf66bb4-c83d-44fc-b414-9819a5de8712 / xfs defaults 0 0
UUID=00398be0-55ba-445f-b7c5-3b2daa383456 /boot xfs defaults 0 0
UUID=bf90b546-0cd9-4c13-ac08-a8860f719bf1 swap swap defaults 0 0
[root@ansible ~]# ansible mysql -m copy -a 'content="MQ" dest=/opt/test.txt'
192.168.88.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "dbd1da18720a652cb47269790ad6ea46174f3df3",
"dest": "/opt/test.txt",
"gid": 0,
"group": "root",
"md5sum": "51f581937765890f2a706c77ea8af3cc",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 2,
"src": "/root/.ansible/tmp/ansible-tmp-1633657435.95-45121-259480666352136/source",
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible mysql -a 'cat /opt/test.txt'
192.168.88.30 | CHANGED | rc=0 >>
MQ
file模块
#创建用户并加入系统组
ansible mysql -m user -a 'name=mysql system=yes'
#修改文件属主为mysql,加入mysql组,并赋权
ansible mysql -m file -a 'owner=mysql group=mysql mode=600 path=/opt/test.txt'
#查看文件是否修改成功
ansible mysql -a 'ls -l /opt/test.txt'
#源文件创建软链接
ansible mysql -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link'
#查看目录
ansible mysql -a 'ls -l /opt'
[root@ansible ~]# ansible mysql -m user -a 'name=mysql system=yes'
192.168.88.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 100,
"home": "/home/mysql",
"name": "mysql",
"shell": "/bin/bash",
"state": "present",
"system": true,
"uid": 990
}
[root@ansible ~]# ansible mysql -m file -a 'owner=mysql group=mysql mode=600 path=/opt/test.txt'
192.168.88.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 985,
"group": "mysql",
"mode": "0600",
"owner": "mysql",
"path": "/opt/test.txt",
"secontext": "system_u:object_r:usr_t:s0",
"size": 2,
"state": "file",
"uid": 990
}
[root@ansible ~]# ansible mysql -a 'ls -l /opt/test.txt'
192.168.88.30 | CHANGED | rc=0 >>
-rw-------. 1 mysql mysql 2 10月 8 09:43 /opt/test.txt
[root@ansible ~]# ansible mysql -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link'
192.168.88.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/opt/test.txt.link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 13,
"src": "/opt/test.txt",
"state": "link",
"uid": 0
}
[root@ansible ~]# ansible mysql -a 'ls -l /opt'
192.168.88.30 | CHANGED | rc=0 >>
总用量 8
-rw-r--r--. 1 root root 501 10月 8 09:43 fstab.back
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
-rw-------. 1 mysql mysql 2 10月 8 09:43 test.txt
lrwxrwxrwx. 1 root root 13 10月 8 09:51 test.txt.link -> /opt/test.txt
ping模块
#检测主机间网络是否通畅
ansible all -m ping
[root@ansible ~]# ansible all -m ping
192.168.88.30 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.88.20 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
service模块
#在web主机上安装apache服务
yum -y install httpd
#查看服务状态
ansible web -a 'systemctl status httpd'
#服务开机自启
ansible web -m service -a 'enabled=true name=httpd state=started'
[root@ansible ~]# ansible web -a 'systemctl status httpd'
192.168.88.20 | FAILED | rc=3 >>
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)non-zero return code
[root@ansible ~]# ansible web -m service -a 'enabled=true name=httpd state=started'
192.168.88.20 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
shell模块
#mysql组中主机创建用户
ansible mysql -m user -a 'name=zhangsan'
#给用户添加密码
ansible mysql -m shell -a 'echo 123456 | passwd --stdin zhangsan'
[root@ansible ~]# ansible mysql -m user -a 'name=zhangsan'
192.168.88.30 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": false,
"comment": "",
"group": 985,
"home": "/home/zhangsan",
"move_home": false,
"name": "zhangsan",
"shell": "/bin/bash",
"state": "present",
"uid": 1234
}
[root@ansible ~]# ansible mysql -m shell -a 'echo 123456 | passwd --stdin zhangsan'
192.168.88.30 | CHANGED | rc=0 >>
更改用户 zhangsan 的密码 。
passwd:所有的身份验证令牌已经成功更新。
script模块
#编写脚本
vim test.sh
#!/bin/bash
echo 'hello ansible from script' > /opt/script.txt
#赋权并使用script模块在所有主机上执行
chmod +x test.sh
ansible all -m script -a 'test.sh'
yum模块
#安装并查看apache服务
ansible mysql -m yum -a 'name=httpd'
ansible mysql -a 'rpm -q httpd'
#卸载apache服务,并再次查看
ansible mysql -m yum -a 'name=httpd state=absent'
ansible mysql -a 'rpm -q httpd'
setup模块
#获取mysql组中主机的facts信息(内容太多,这里就不截图了)
ansible mysql -m setup
#facts远程系统信息,包含IP地址,操作系统,以太网设备,MAC地址,时间/日期,硬件等信息。