目录
Ansible是一种自动化的运维工具,基于Python开发,它集合了众多运维工具(比如puppet、chef、func等)的优点,能够实现批量操作。但其实Ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是Ansible所运行的模块,Ansible只是提供一种框架。
安装Ansible
yum install epel-release
yum repolist
# 若安装失败 则依次安装依赖
yum install -y ansible
查看Ansible版本
ansible --version
ansible 2.9.10
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 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
定义Ansible组
修改 /etc/ansible/hosts 文件中内容 ,添加组,如下:
vi /etc/ansible/hosts
# 接下来定义Ansible模块
# ansible_ssh_port 连接目标远程主机的端口
# ansible_ssh_user 连接目标远程主机默认用户
# ansible_ssh_pass 连接目标远程主机默认用户密码
# 定义组 - bdp
[bdp]
192.168.254.121 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.122 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.123 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.124 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.125 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
192.168.254.126 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='gis@pwd'
# 修改该文件中 host_key_checking = False (避免SSH_KEY问题)
vi /etc/ansible/ansible.cfg
测试Ansible Command&shell
ansible bdp -m command -a "free -m"
ansible bdp -m command -a "cat /etc/sysconfig/network-scripts/ifcfg-ens33"
ansible bdp -m shell -a "cat /etc/sysconfig/network-scripts/ifcfg-ens33"
接下来学习下基于Ansible批量创建用户与用户组
Group 模块
Group 模块主要用来添加或者删除组。
ansible-doc -s group
- name: Add or remove groups
group:
gid: # Optional `GID' to set for the group.
local: # Forces the use of "l