自动化服务配置管理平台之-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
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
相关文章推荐
- 配置Tomcat,允许指定身份用户管理服务(Linux平台)
- Java Web学习总结(23)——Distributed Configuration Management Platform(分布式配置管理平台)
- 集中化管理平台saltstack——自动化实现apache的安装与配置
- 开源自动化配置管理工具ansible、saltstack、Puppet、Chef选择
- Ansible :一个配置管理和IT自动化工具
- 在浙江杭州快速开发公司内部办公自动化OA系统的经验总结(有成熟的后台配置工具、开发平台、程序员朋友圈还真不错,做软件项目还得靠积累才稳妥)
- Ansible :一个配置管理和IT自动化工具
- Ansible安装与配置(自动化运维管理工具)
- 服务自动化部署平台之Saltstack总结
- OSSIM下自动化配置管理工具Ansible实战
- Java Web学习总结(23)——Distributed Configuration Management Platform(分布式配置管理平台)
- 自动化运维管理工具ansible的配置与使用
- Puppet系列之一:自动化配置管理平台介绍
- 服务自动化部署平台之Saltstack总结
- Ansible :一个配置管理和IT自动化工具
- Ansible 运维自动化 ( 配置管理工具 )
- Ansible :一个配置管理和IT自动化工具
- 5、python自动化运维——集中化管理平台Ansible
- javaweb学习总结十八(软件密码学、配置tomcat的https连接器以及tomcat管理平台)
- Ansible :一个配置管理和IT自动化工具