ansible安裝配置

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
ansible安裝配置
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解釋器
ansible安裝配置
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:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章