Ansible架构:python
ansible是新出现的运维工具是基于Python研发的糅合了众多老牌运维工具的优势实现了批量操做系统配置、批量程序的部署、批量运行命令等功能。linux
和同类工具puppet和saltstack比起来优势是更易于管理,不须要安装客户端(经过ssh链接通讯)web
ansible搭建以及配置:
1.ansible的安装
#yum install ansible -y
[root@wy-pe1 ~]# rpm -ql ansible | head
/etc/ansible
/etc/ansible/ansible.cfg ansible的主配置文件
/etc/ansible/hosts ansible的Host Inventoy文件
/etc/ansible/roles
/usr/bin/ansible
/usr/bin/ansible-doc 模块相关的命令
/usr/bin/ansible-galaxy
/usr/bin/ansible-playbook playbook相关命令
/usr/bin/ansible-pull
/usr/bin/ansible-vault
2.定义Host Inventory
# vim /etc/ansible/hosts
www.xxbandy.com 定义单个域名或主机
172.25.25.4
[webshosts] 定义一个服务群组(能够用来区分服务群组)
10.45.249.119 ansible_ssh_user=root ansible_ssh_pass=wyadmin
10.45.249.121 ansible_ssh_user=root ansible_ssh_pass=wyadmin
注意:
ansible_ssh_user=root 定义ssh登陆用户
ansible_ssh_pass=wyadmin 定义ssh登陆密码
ansible_connection=ssh/local 定义主机链接类型
同时,必须主机必须记录过一次ssh的密码
经过隧道进行安全链接:
xxbandy ansible_ssh_port=55555 ansible_ssh_host=172.25.25.250
ansible中能够使用range:
www[01:90].xxbandy.com
#ansible webshosts -a ‘/sbin/reboot’ -f 10 当即重启webshosts中的主机
3.测试各个模块
ansible命令最经常使用的用法:
ansible <Host-partten> -m MOD -a 'command'
所支持的模块能够使用ansible-doc -l查看
ansible简单使用示例:
简单使用copy模块(注意,其中须要在各个主机上安装libselinux-python包)
# ansible webshosts -m copy -a 'src=/root/command dest=/mnt'
10.45.249.119 | success >> {
"changed": false,
"checksum": "704ac4e439698bc9f1a314a9fce18fb658804d7c",
"dest": "/mnt/command",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/mnt/command",
"secontext": "system_u:object_r:mnt_t:s0",
"size": 79136,
"state": "file",
"uid": 0
}
10.45.249.121 | success >> {
"changed": true,
"checksum": "704ac4e439698bc9f1a314a9fce18fb658804d7c",
"dest": "/mnt/command",
"gid": 0,
"group": "root",
"md5sum": "b918049ba2652e9eb7bc1ad77e8cb8e4",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:mnt_t:s0",
"size": 79136,
"src": "/root/.ansible/tmp/ansible-tmp-1425651119.69-276531752760623/source",
"state": "file",
"uid": 0
}
表示文件已经向webshosts组内的主机同步成功
使用command或者shell模块进行远程调用系统命令:
[root@wy-pe1 mnt]# ansible webshosts -m command -a 'date'
10.45.249.119 | success | rc=0 >>
Fri Mar 6 22:15:09 CST 2015
10.45.249.121 | success | rc=0 >>
Fri Mar 6 21:56:03 CST 2015
[root@wy-pe1 mnt]# ansible webshosts -m command -a 'df -h'
10.45.249.121 | success | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 917M 16G 6% /
tmpfs 238M 0 238M 0% /dev/shm
/dev/sda1 477M 25M 427M 6% /boot
10.45.249.119 | success | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 936M 16G 6% /
tmpfs 238M 0 238M 0% /dev/shm
/dev/sda1 477M 25M 427M 6% /boot
在控制端添加用户,user模块:
查看user模块的相关参数(ansible-doc user)
# ansible webshosts -m user -a 'name=andy comment="ansible add use" uid=1001 password=andyxxb'
参数详解:name指定用户名
comment指定描述
uid指定用户ID(gid)
password指定用户密码
shell指定用户所拥有的shell
state=absent用来删除用户(只会删除本地用户,不会删除家目录)
实现ssh秘钥认证:(其实就是在ansible端进行key生成(ssh-keygen),而后使用copy模块进行同步文件)
#ansible webshosts -m copy -a 'src=/root/.ssh/id_rsa.pub dest=/root'
# ansible webshosts -m shell -a 'cat /root/id_rsa.pub >> /root/.ssh/authorized_keys'
file模块:用来改变文件的权限和所属用户组;同时还能够建立目录(没有成功),删除文件等
# ansible webshosts -m file -a 'dest=/mnt/command mode=600 owner=xxb group=xxb '
10.45.249.119 | success >> {
"changed": true,
"gid": 500,
"group": "xxb",
"mode": "0600",
"owner": "xxb",
"path": "/mnt/command",
"secontext": "system_u:object_r:mnt_t:s0",
"size": 79136,
"state": "file",
"uid": 500
}
10.45.249.121 | success >> {
"changed": true,
"gid": 502,
"group": "xxb",
"mode": "0600",
"owner": "xxb",
"path": "/mnt/command",
"secontext": "system_u:object_r:mnt_t:s0",
"size": 79136,
"state": "file",
"uid": 502
}
使用file模块进行建立目录:(mkdir -p)
# ansible webshosts -m file -a "dest=/mnt/hello mode=755 owner=xxb group=xxb state=directory"
使用file模块进行删除文件
# ansible webshosts -m file -a 'dest=/mnt/command state=absent'
使用yum模块进行安装软件包,肯定包已经安装,可是不进行udate操做:
# ansible webshosts -m yum -a 'name=vsftpd state=installed'
注意(state=latest用来确保软件包是否为最新版本)
使用service模块进行肯定服务的运行状态:
#ansible webshosts -m yum -a 'name=httpd state=installed'
# ansible webshosts -m service -a 'name=httpd state=restarted'
注意:state的状态为restarted,started,stoped
shell