Ansible主控机ip:192.168.1.7 hostname : ansible.westos.com
受控机三台:
i
p
ip
ip:192.168.1.107
h
o
s
t
n
a
m
e
hostname
hostname : test1.westos.com
i
p
ip
ip : 192.168.1.108
h
o
s
t
n
a
m
e
hostname
hostname : test2.westos.com
i
p
ip
ip : 192.168.1.109
h
o
s
t
n
a
m
e
hostname
hostname : test3.westos.com
1.Ansible对于企业运维的重大意义
Ansible是一个简单高效的自动化运维管理工具,用Python开发(安装时依赖Python版本,控制主机master器要求Python2.6以上,被控制的主机server需要Python2.6以上。centos6.8自带Python2.6),能大批量管理N多台机器,可以并发的在多台机器上部署应用、安装软件、执行命令、配置和编排任务。
2.Ansible的安装
-
配置 e p e l epel epel源:
-
d n f dnf dnf i n s t a l l install install a n s i b l e − y ansible -y ansible−y
-
查看 a n s i b l e ansible ansible版本:
-
ansible的基本信息:
/etc/ansible/ansible.conf | 全局配置文件,默认很少修改 |
---|---|
/etc/ansible/hosts | 全局主机清单清单文件 |
3.构建Ansible清单
- 清单就是ansible控制主机的列表:
/ e t c / a n s i b l e / h o s t s : /etc/ansible/hosts: /etc/ansible/hosts:全局清单文件
-
直接书写受管主机ip或hostname,每行一个;
-
在ansible主机中做好相应的解析:
v i m / e t c / h o s t s vim /etc/hosts vim/etc/hosts
-
清单查看:
ansible 清单中组名称 [-i 清单文件] --list-hosts |
---|
ansible ungrouped --list-host |
ansible all --list-hosts |
ansible -inventory --list all |
- 设定受管主机的组[组名称]
- 主机规格的范围化操作
- ansible命令指定清单的正则表达式
4.Ansible配置文件参数详解
- ansible 清单中组名称 -m 模块 -u remote_user
- 配置文件的分类与优先级
/etc/ansible/ansible.cfg | 基本配置文件,找不到其他配置文件此文件生效 |
---|---|
~/.ansible.cfg | 用户当前目录中没有ansible.cfg此文件生效 |
./ansible.cfg | 优先级最高 |
- 常用配置参数
5.构建用户级Ansible操作环境
-
su - yang : 切换普通用户
-
cp /etc/ansible/ansible.cfg /home/yang/
-
vim /home/yang/ansible.cfg
-
-
vim inventory
-
-
ansible all --list-hosts
-
-
构建用户级ansible只能在指定含有ansible.cfg目录下执行ansible命令