您的位置:首页 > 运维架构 > Linux

自动化服务配置管理平台之-Ansible总结

2015-03-25 00:00 459 查看
摘要: Ansible 提供一种最简单的方式用于发布、管理和编排计算机系统的工具,你可在数分钟内搞定。
Ansible 是一个模型驱动的配置管理器,支持多节点发布、远程任务执行。默认使用 SSH 进行远程连接。无需在被管理节点上安装附加软件,可使用各种编程语言进行扩展。

Ansible架构:



ansible是新出现的运维工具是基于Python研发的糅合了众多老牌运维工具的优点实现了批量操作系统配置、批量程序的部署、批量运行命令等功能。

和同类工具puppet和saltstack比起来优点是更易于管理,不需要安装客户端(通过ssh连接通信)

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息