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/hosts

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)参数如下:

名称 默认值 描述 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: