5.ansible安装
(1)依赖于epel源,可是能够使用yum解决依赖关系
[root@pythion ~]# yum -y install ansible openssh* libselinux-python
[root@rsync yml]# yum -y update ca-certificates --disablerepo=epel
(2)也能够源码安装,全部的模块都得编译安装,进入安装目录后执行python setup.py install
6.ansible配置
6.1 主配置文件
[root@pythion ~]# ls /etc/ansible/ansible.cfg
6.2 Invertory
有两种方式,第一种不改配置文件,全部的都在hosts文件中,另外一种是修改配置文件,能够把这些放到不一样的文件中去
[root@pythion ~]# ls /etc/ansible/hostspython
6.2.1简介
(1)ansible的主要功能在于批量主机操做,为了便捷的使用其中的部分主机,能够在inventory file中将其分组命名,默认的inventory file为/etc/ansible/hosts
(2)Inventory file能够有多个,且也能够经过Dynamic inventory来动态生成
(3)遵循INI文件风格,括号中的字体为组名
6.2.2添加主机
(1)能够将同一个主机同时归并到多个不一样的组中
(2)当如若目标主机使用了非默认的ssh端口,还能够在主机名称以后使用冒号加端口来标明
[beijing]
www.it211.com.cn
v.it211.com.cn
[shanghai]
zhang.it211.com.cn
wang.it211.com.cn
若是主机名遵循类似的命名模式,还能够使用列表的方式标识各主机,例如
[beijing]
www[01:50].example.com.cn
[wang]
db-[a:f].example.com.cn
6.2.3 ssh参数
(1)ansible基于ssh链接inventory中指定的远程主机时,还能够经过参数指定其交互方式(2)参数以下:linux
名称 默认值 描述
ansible_ssh_host 主机的名字 SSH目的主机名或IP
ansible_ssh_port 22 SSH目的端口
ansible_ssh_user root SSH登陆使用的用户名
ansible_ssh_pass none SSH认证所使用的密码
ansible_connection smart ansible使用何种链接模式链接到主机
ansible_ssh_private_key_file none SSH认证所使用的私钥
ansible_shell_type sh 命令所使用的shell
ansible_python_interpreter /usr/bin/python 主机上的python解释器
6.2.4方式
[root@zabbix-server inventory]# vim /etc/ansible/ansible.cfg
inventory = /etc/ansible/inventory
[root@zabbix-server inventory]# pwd
/etc/ansible/inventory
[root@zabbix-server inventory]# ls
31 32 69 70 all test
能够把每个分组的都写成一个文件,固然也能够再建一级目录,好比测试,开发,线上,用来进行更详细的划分,就是把不一样环境的主机或者不一样业务的主机放在不一样的inventory文件中。能够验证的看下
[root@zabbix-server inventory]# ansible test --list-hosts
hosts (9):
192.168.70.50
192.168.70.51
192.168.70.52
192.168.70.57
192.168.70.74
192.168.70.78
192.168.70.93
192.168.70.90
192.168.70.86
6.3 yaml文件定义
也能够在yml文件中定义hosts,格式以下
[root@zabbix-server ~]# vim host.yml
all:
children:
pro:
children:
proA:
hosts:
10.0.0.1:
proB:
hosts:
10.0.0.2:shell