Ansible專案
一、Ansible簡介:
ansible是一種新流行的自動化運維工具,基于python2-paramiko模塊開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實作了批量系統配置、批量程式部署、批量運行命令功能,
ansible是基于模塊作業的,本身沒有批量部署的能力,真正具有批量部署的是ansible所運行的模塊,ansible只提供一種框架,ansible這個框架主要包含以下功能:
(1)連接插件connection plugins:負責和被監控端事先通信;
(2)host inventory:操作主機清單;
(3)核心模塊、command模塊、自定義模塊;
(4)借助與插件完成記錄日志郵件等功能;
(5)Playbook:劇本執行多個任務時,非必須可以讓節點一次性運行多個任務,
二、Ansible特性:
(1)No agents: 不需要在被管理主機上安裝任何客戶端軟體,
(2)No server: 無服務器端的概念,使用時直接運行命令即可,
(3)Modules in any languages: 基于模塊作業,可使用任意語言開發模塊,
(4)Yaml, not code: 使用yaml語言定制劇本playbook,
(5)Ssh by default: 基于SSH作業,
(6)Strong multi-tier solution: 可實作多級指揮,
三、Ansible優點:
(1)輕量級,無需在客戶端安裝agent,更新時,只需在操作機上進行一次更新即可,
(2)批量任務執可以寫成腳本,而且不用分發到遠程就可以執行,
(3)使用python撰寫,維護更簡單,ruby語法過于復雜,
(4)支持sudo
四、安裝Ansible:
(1)管理節點上安裝Ansible
(Redhat/CentOS Linux上,Ansible目前放在epel源中,Fedora默認源中包含Ansible,自己安裝即可)
# yum install epel-release
# yum install ansible -y
# ssh-keygen
# ssh-copy-id remoteuser@remotehost
(2)被管理的遠程主機
(不需要安裝特殊的包,只需要配置并啟動SSH服務且Python版本在2.4以上即可,Redhat一般安裝方式都是默認安裝的,)
五、Ansible組態檔簡介:
(1)主組態檔:/etc/ansible/ansible.cfg
(2)默認主機清單檔案:/etc/ansible/hosts
Ansible的組態檔說明:
|
組態檔種類 |
說明 |
所在位置 |
|
默認組態檔 |
ansible軟體的的默認組態檔,對所有使用ansible的用戶生效 |
/etc/ansible/ansible.cfg |
|
用戶組態檔 |
使用普通用戶的執行特定的運維playbook,只要使用該用戶登錄ansible運維主機,不管在任何檔案夾下該組態檔將會覆寫默認組態檔中的內容, |
~/amsible.cfg ~/.ansible.cfg |
|
運維專案組態檔 |
把一類運維playbook放在一個檔案夾中,僅僅在當前的運維專案中生效的組態檔, |
/dir/ansible.cfg |
● 使用ansible --version查看當前生效的ansible.cfg的路徑,
● 當三種檔案的發生沖突的時候,優先級:運維專案組態檔 > 用戶組態檔 > 默認組態檔
/dir/ansible.cfg > ~/.ansible.cfg > ~/amsible.cfg > /etc/ansible/ansible.cfg
可以在ansible.cfg指定hosts檔案在哪里
六、Ansible默認主機清單檔案Inventory為/etc/ansible/hosts:
什么是inventory:
● Static lines of servers
● Ranges
● Other custom things
● Dynamic lists of servers: AWS,Azure,GCP,etc.
語法結構:
● “#”開始表示注釋,空格行被忽略
● 使用[ ]設定遠程主機分組,[groupname]內是組名,
● 主機串列可以使用主機名或IP地址,
● 一個主機名或IP地址可以在多個分組中,
● 使用[start:end]表示連續的主機,
● 分組支持嵌套,一個組可以包含其它組,新組名中以”:children“后綴,
[group1]
host1.example.com
host2.example.com
[group2]
host3.example.com
host4.example.com
[newgroup:children]
group1
group2
(1)標準的主機和分組清單例子:
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
(2)非標準埠下主機清單
badwolf.example.com:5309
(3)連續大量主機清單"[start:end]"
[webservers]
www[01:50].example.com
[databases]
db-[a:f].example.com
192.168.[4:7].[0:255] //在192.168.4.0/22網段下的所有主機,即192.168.4.0-192.168.7.255
(4)主機設定別名的清單
jumper ansible_port=5555 ansible_host=192.0.2.50
(5)組中包含其它組(組嵌套)
[olympia]
washington1.example.com
Washington2.example.com
[salem]
oregon01.example.com
oregon02.example.com
[nwcapitols:children]
olympia
salem
(6)設定連接引數
[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_user=mdehaan
主機清單檔案常用連接引數
|
連接引數 |
表示含義 |
|
ansible_connection |
SSH的連接方式,可以指定為smart、ssh、local、paramiko |
|
ansible_host |
Ansible連接的主機地址,如果在主機清單中起了一個不同的別名,那么需要用這個引數指定主機IP或主機名, |
|
ansible_port |
SSH埠號,默認為22, |
|
ansible_user |
SSH連接時使用的默認用戶名, |
|
ansible_ssh_pass |
SSH連接時使用的密碼,不過不建議用本引數存盤明文的密碼, |
|
ansible_ssh_private_key_file |
指定使用ssh-keygen生成的私鑰檔案所存放的位置, |
|
ansible_ssh_common_args |
通過配置此引數來指定SFTP、SCP和SSH默認的額外引數, |
更多連接引數http://docs.ansible.com/ansible/latest/intro_inventory.html#list-of-behavioral-inventory-parameters
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/148080.html
標籤:Linux
下一篇:[Linux] linux路由表
