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