Ansible--快速入門

Ansible快速入門

介紹

Ansible是一款簡單的運維自動化工具,只須要使用ssh協議鏈接就能夠來進行系統管理,自動化執行命令,部署等任務。html

Ansible的特色node

一、ansible不須要單獨安裝客戶端,也不須要啓動任何服務
二、ansible是python中的一套完整的自動化執行任務模塊
三、ansible playbook 採用yaml配置,對於自動化任務執行過一目瞭然python

Ansible組成結構linux

    • 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)配置epelnginx

[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)安裝ansibleweb

[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中文文檔docker

Inventory文件一般用於定義要管理的主機的認證信息,例如ssh登陸用戶名、密碼以及key相關信息。能夠同時操做一個組的多臺主機,組與主機組之間的關係都是經過inventory文件配置。配置文件路徑爲:/etc/ansible/hostsshell

基於密碼鏈接

[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"

基於祕鑰鏈接

基於祕鑰鏈接須要先建立公鑰和私鑰,併發送給被管理機器apache

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"}
相關文章
相關標籤/搜索