|
一、ansible是基于python paramiko开发,分布式,无需安装客户端,轻量级的自动化运维管理工具。
二、ansible的安装和配置
yum安装ansible
vim /etc/ansible/hosts
1
2
| [test] \\主机组名,可以任意命名
192.168.2.101 \\管理节点主机,也可以是主机名
|
修改/etc/ansible/ansible.cfg
1
| host_key_checking = False
|
- 加快ssh的速度,强制用ssh,防止自动选择用python的
1
2
3
4
| #找到transport,修改成ssh
transport = ssh
#找到ssh_args,去掉注释,修改成空,在[ssh_connection]的后面
ssh_args =
|
三、ansible的常用模块
1. commom模块,默认模块,用于在各被管理节点运行指令的命令
1
2
3
4
5
6
7
8
9
| [iyunv@test ~]# ansible all -m command -a 'ifconfig eth0'
192.168.2.101 | success | rc=0 >>
eth0 Link encap:Ethernet HWaddr 00:0C:29:F8:D4:88
inet addr:172.16.2.13 Bcast:172.16.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:fef8:d488/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8046 errors:0 dropped:0 overruns:0 frame:0
TX packets:2165 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:1392192 (1.3 MiB) TX bytes:201703 (196.9 KiB)
|
2. user模块,用户模块,用于在各被管理节点管理用户所使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [iyunv@test ~]# ansible all -m user -a 'name=test'
192.168.2.101 | success >> {
"changed": true,
"comment": "",
"createhome": true,
"group": 500,
"home": "/home/test",
"name": "test",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 500}
[iyunv@test ~]# tail -1 /etc/passwd
test:x:500:500::/home/test:/bin/bash
|
3. group模块,用户组模块,用于在各被管理节点管理用户组所使用
1
2
3
4
5
6
7
8
| [iyunv@test ~]# ansible all -m group -a 'name=mylinux gid=1000'
192.168.2.101 | success >> { "changed": true,
"gid": 1000,
"name": "mylinux",
"state": "present",
"system": false}
[iyunv@test ~]# tail -1 /etc/gshadow
mylinux:!::
|
4. cron模块,计划任务模块,用于在各被管理节点管理计划任务
1
2
3
4
5
6
7
8
9
| [iyunv@test ~]# ansible all -m cron -a "name=time minute='*/2' job='/usr/sbin/ntpdate 192.168.2.100'"
192.168.2.101 | success >> { "changed": true,
"jobs": [
"time"
]
}
[iyunv@test ~]# crontab -l \\在管理节点查看cron任务
#Ansible: time
*/2 * * * * /usr/sbin/ntpdate 192.168.2.100
|
5. copy模块:复制模块,复制文件至各管理节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [iyunv@test ~]# ansible all -m copy -a 'src=/root/test dest=/tmp mode=600'
192.168.2.101 | success >> { "changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/tmp/test",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0600",
"owner": "root",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1439189042.77-131108212586927/source",
"state": "file",
"uid": 0}
[iyunv@test ~]# ls -l /tmp/test
-rw------- 1 root root 0 Aug 10 14:44 /tmp/test
|
6. file模块:文件模块,修改各个节点指定的文件属性
1
2
3
4
5
6
7
8
9
10
11
12
13
| [iyunv@test ~]# ansible all -m file -a 'path=/tmp/test mode=644 owner=test'
192.168.2.101 | success >> { "changed": true,
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "test",
"path": "/tmp/test",
"size": 0,
"state": "file",
"uid": 500
}
[iyunv@test ~]# ls -l /tmp/test
-rw-r--r-- 1 test root 0 Aug 10 14:44 /tmp/test
|
7. ping模块:测试模块,测试各个被管理节点是否在线
1
2
3
4
5
| [iyunv@test ~]# ansible all -m ping
192.168.2.101 | success >> {
"changed": false,
"ping": "pong"
}
|
8. service模块:管理各个节点的服务
1
2
3
4
5
6
| [iyunv@test ~]# ansible all -m service -a 'name=ntpd enabled=true'
192.168.2.101 | success >> {
"changed": true,
"enabled": true,
"name": "ntpd"
}
|
9. shell模块:与command模块功能相同,但比command的模块功能强大
1
2
3
4
| [iyunv@test ~]# ansible all -m shell -a 'cat /etc/passwd | grep root'
192.168.2.101 | success | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
|
10. script模块:自动复制脚本至远程节点并运行
1
2
3
4
5
6
7
8
9
10
11
| [iyunv@test ~]# cat ansible.sh
#!/bin/bashecho "hello word" >> /tmp/test
[iyunv@zqq ~]# ansible all -m script -a '/root/ansible.sh'
192.168.2.101 | success >> {
"changed": true,
"rc": 0,
"stderr": "...",
"stdout": ""
}
[iyunv@test ~]# cat /tmp/test
hello word
|
11. setup模块:收集ansible的facters
1
2
3
4
5
6
7
| [iyunv@test ~]# ansible all -m setup
192.168.2.101 | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.2.101"
],
...............
|
12. yum模块:用于在各个管理节点安装软件所使用
1
2
3
4
5
6
7
8
9
| [iyunv@test ~]# ansible all -m yum -a 'name=httpd state=present'
192.168.2.101 | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"...
[iyunv@test ~]# rpm -q httpd
httpd-2.2.15-45.el6.centos.x86_64
|
|
|
|