ansible是一种由Python开发的自动化运维工具,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible环境搭建
需要三台主机,一台管理主机,两台被管理主机
主机环境:
1.静态ip
2.关闭防火墙
3.关闭selinux
4.要epel源
实验过程
第1步: 管理机上安装ansible,被管理节点必须打开ssh服务。
yum -y install ansible
ansible --version
ansible 2.9.27
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
第2步: 实现master对agent的免密登录,只在master上做。(如果这 ⼀步不做,则在后⾯操作agent时都要加-k参数传密码;或者在主机清 单⾥传密码)
ssh-keygen
ssh-copy-id -i 192.168.1.63
ssh-copy-id -i 192.168.1.64
第3步: 在master上定义主机组,并测试连接性
vim /etc/ansible/hosts
[group01]
192.168.1.63
192.168.1.64
[group02]
192.168.1.63
192.168.1.64
192.168.1.65
ping模块
ansible 192.168.1.63 -m ping
192.168.1.63 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ansible模块
查看所有⽀持的模块
ansible-doc -l
官⽹模块⽂档地址: https://docs.ansible.com/ansible/latest/modules /list_of_all_modules.html
hostname模块
hostname模块⽤于修改主机名(注意: 它不能修改/etc/hosts⽂件)
#将其中⼀远程机器主机名修改为agent1.cluster.com
ansible 192.168.1.63 -m hostname -a 'name=agent1.cluster.com'
创建⼀个⽬录
ansible group1 -m file -a 'path=/test state=directory'
创建⼀个⽂件
ansible group1 -m file -a 'path=/test/111 state=touch'
递归修改owner,group,mode
ansible group1 -m file -a 'path=/test recurse=yes owner=bin group=daemon mode=1777'
删除⽬录(连同⽬录⾥的所有⽂件)
ansible group1 -m file -a 'path=/test state=absent'
创建⽂件并指定owner,group,mode等
ansible group1 -m file -a 'path=/tmp/111 state=touch owner=bin group=daemon mode=1777'
删除⽂件
ansible group1 -m file -a 'path=/tmp/111 state=absent'