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文件通常用於定義要管理的主機的認證信息,例如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 —— 臨時的,在ansible中是指需要快速執行,並且不需要保存的命令。說白了就是執行簡單的命令——一條命令。對於復雜的命令則為playbook,類似於saltstack的state 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"}
