1. 程式人生 > 實用技巧 >Ansible入門

Ansible入門

Ansible快速入門#

介紹#

Ansible是一款簡單的運維自動化工具,只需要使用ssh協議連線就可以來進行系統管理,自動化執行命令,部署等任務。

Ansible的特點

1、ansible不需要單獨安裝客戶端,也不需要啟動任何服務
2、ansible是python中的一套完整的自動化執行任務模組
3、ansible playbook 採用yaml配置,對於自動化任務執行過一目瞭然

Ansible組成結構

    • Ansible
      Ansible的命令工具,核心執行工具;一次性或臨時執行的操作都是通過該命令執行。
    • Ansible Playbook
      任務劇本(又稱任務集),編排定義Ansible任務集的配置檔案,由Ansible
      順序依次執行,yaml格式。
    • Inventory
      Ansible管理主機的清單,預設是/etc/ansible/hosts檔案。
    • Modules
      Ansible執行命令的功能模組,Ansible2.3版本為止,共有1039個模組。還可以自定義模組。
    • Plugins
      外掛,模組功能的補充,常有連線型別外掛,迴圈外掛,變數外掛,過濾外掛,外掛功能用的較少。
    • API
      提供給第三方程式呼叫的應用程式程式設計介面。

環境準備#

IP系統主機名描述
192.168.1.30 CentOS7 ansible ansible管理節點
192.168.1.31 CentOS7 linux.node01.com 被管理節點1
192.168.1.32 CentOS7 linux.node02.com 被管理節點2
192.168.1.33 CentOS7 linux.node03.com 被管理節點3
192.168.1.36 CentOS6 linux.node06.com 被管理節點6

Ansible安裝#

1)配置epel

[root@ansible ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ansible ~]# yum clean all
[root@ansible ~]# yum makecache

2)安裝ansible

[root@ansible ~]# yum -y install ansible

# 檢視ansible版本
[root@ansible ~]# ansible --version
ansible 2.8.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

Ansible Inventory檔案#

Inventory中文文件

Inventory檔案通常用於定義要管理的主機的認證資訊,例如ssh登入使用者名稱、密碼以及key相關資訊。可以同時操作一個組的多臺主機,組與主機組之間的關係都是通過inventory檔案配置。配置檔案路徑為:/etc/ansible/hosts

基於密碼連線#

[root@ansible ~]# vim /etc/ansible/hosts
# 方法一 主機+埠+密碼
[webserver]
192.168.1.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.32 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.33 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.36 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"


# 方法二 主機+埠+密碼
[webserver]
192.168.1.3[1:3] ansible_ssh_user=root ansible_ssh_pass="123456"


# 方法二 主機+埠+密碼
[webserver]
192.168.1.3[1:3]
[webserver:vars]
ansible_ssh_pass="123456"

基於祕鑰連線#

基於祕鑰連線需要先建立公鑰和私鑰,併發送給被管理機器

1)生成公私鑰

[root@ansible ~]# ssh-keygen
[root@ansible ~]# for i in {1,2,3,6}; do ssh-copy-id -i 192.168.1.3$i ; done

2)配置連線

[root@ansible ~]# vim /etc/ansible/hosts
# 方法一 主機+埠+金鑰
[webserver]
192.168.1.31:22
192.168.1.32
192.168.1.33
192.168.1.36

# 方法一 別名主機+埠+金鑰
[webserver]
node1 ansible_ssh_host=192.168.1.31 ansible_ssh_port=22
node2 ansible_ssh_host=192.168.1.32 ansible_ssh_port=22
node3 ansible_ssh_host=192.168.1.33 ansible_ssh_port=22
node6 ansible_ssh_host=192.168.1.36 ansible_ssh_port=22

主機組的使用#

# 主機組變數名+主機+密碼
[apache]
192.168.1.36
192.168.1.33
[apache.vars]
ansible_ssh_pass='123456'

# 主機組變數名+主機+金鑰
[nginx]
192.168.1.3[1:2]

# 定義多個組,把一個組當另外一個組的組員
[webserver:children]  #webserver組包括兩個子組:apache nginx
apache
nginx

臨時指定inventory#

1)先編輯一個主機定義清單

[root@ansible ~]# vim /etc/dockers
[dockers]
192.168.1.31 ansible_ssh_pass='123456'
192.168.1.32
192.168.1.33

2)在執行命令是指定inventory

[root@ansible ~]# ansible dockers -m ping -i /etc/dockers -o 
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

Inventory內建引數#

Ansible Ad-Hoc#

Ad-Hoc中文文件

ad-hoc—— 臨時的,在ansible中是指需要快速執行,並且不需要儲存的命令。說白了就是執行簡單的命令——一條命令。對於複雜的命令則為playbook,類似於saltstackstate sls狀態檔案。

ansible命令格式#

1)常用命令引數

[root@ansible ~]# ansible -h
Usage: ansible <host-pattern> [options]
-a MODULE_ARGS   #模組引數
-C, --check  #檢查語法
-f FORKS #併發
--list-hosts #列出主機列表
-m MODULE_NAME #模組名字
-o 使用精簡的輸出

2)示例

[root@ansible ~]# ansible webserver -m shell -a 'uptime' -o
192.168.1.36 | CHANGED | rc=0 | (stdout)  13:46:14 up 1 day,  9:20,  4 users,  load average: 0.00, 0.00, 0.00
192.168.1.33 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:51,  3 users,  load average: 0.00, 0.01, 0.05
192.168.1.31 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:50,  3 users,  load average: 0.00, 0.01, 0.05
192.168.1.32 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:59,  3 users,  load average: 0.00, 0.01, 0.05

3)命令說明

host-pattern格式#

目標target主機,主機組匹配方式

主機的匹配

#  一臺目標主機
[root@ansible ~]# ansible 192.168.1.31 -m ping

# 多臺目標主機
[root@ansible ~]# ansible 192.168.1.31,192.168.1.32 -m ping

# 所有目標主機
[root@ansible ~]# ansible all -m ping

組的匹配

# 組的配置資訊如下:這裡定義了一個nginx組和一個apache組
[root@ansible ~]# ansible nginx --list
  hosts (2):
    192.168.1.31
    192.168.1.32
[root@ansible ~]# ansible apache --list
  hosts (3):
    192.168.1.36
    192.168.1.33
    192.168.1.32

# 一個組的所有主機匹配
[root@ansible ~]# ansible apache -m ping

# 匹配apache組中有,但是nginx組中沒有的所有主機
[root@ansible ~]# ansible 'apache:!nginx' -m ping -o
192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

# 匹配apache組和nginx組中都有的機器(並集)
[root@ansible ~]# ansible 'apache:&nginx' -m ping -o
192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

# 匹配apache組nginx組兩個組所有的機器(並集);等於ansible apache,nginx -m ping
[root@ansible ~]# ansible 'apache:nginx' -m ping -o
192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}