ansible自動化工具(一)

Ansible 是什麼

   Ansible 簡單的說是一個配置管理系統(configuration management system)。你只須要可使用 ssh 訪問你的服務器或設備就行。它也不一樣於其餘工具,由於它使用推送的方式,而不是像 puppet 等 那樣使用拉取安裝agent的方式。你能夠將代碼部署到任意數量的服務器上!node

Ansible能作什麼

  Ansible能夠幫助咱們完成一些批量任務,或者完成一些須要常常重複的工做。 好比:同時在100臺服務器上安裝nginx服務,並在安裝後啓動它們。 好比:將某個文件一次性拷貝到100臺服務器上。 好比:每當有新服務器加入工做環境時,你都要爲新服務器部署某個服務,也就是說你須要常常重複的完成相同的工做。 這些場景中咱們均可以使用到ansible。python

Ansible特性

  • 模塊化:調用特定的模塊,完成特定的任務
  • 有Paramiko,PyYAML,Jinja2(模板語言)三個關鍵模塊
  • 支持自定義模塊
  • 基於Python語言實現
  • 部署簡單,基於Python和SSH(默認已經安裝),agentless
  • 安全,基於OpenSSH
  • 支持Playbook編排任務
  • 冪等性,一個任務執1編和執行n編效果同樣,不因重複執行帶來意外狀況
  • 無需代理不依賴PKI(無需ssl)
  • 可以使用任何編程語言寫的模塊
  • YAML格式,編排任務,支持豐富的數據結構
  • 較強大的多層解決方案 

Ansible架構

Ansible工做原理

Ansible主要組成部分功能說明

 PLAYBOOKS:linux

    任務劇本(任務集),編排定義Ansible任務集的配置文件,由Ansible順序依次執行,一般是JSON格式的YML文件nginx

INVENTORY:web

    Ansible管理主機的清單,/etc/ansible/hosts正則表達式

MODULES:shell

    Ansible執行命令的功能模塊,多數爲內置模塊,也能夠自定義,ansible-doc -l可查看模塊編程

PLUGINS:json

    模塊功能的補充,如鏈接類型插件,循環插件,變量插件,過濾插件等,不經常使用windows

API:

    提供第三方程序調用的應用程序編程接口

ANSIBLE:

    組合INVENTORY、API、MODULES、PLUGINS的,能夠理解爲是ansible的命令工具,其爲核心執行工具

注意事項:

  • 執行ansible的主機通常稱爲主控端,中控端,master或者堡壘機
  • 主控端Python版本須要2.6以上
  • 被控端python版本小於2.4則須要安裝python-simplejson
  • 被控端若是開啓selinux,須要安裝libselinux-python
  • windows不能作爲主控端

安裝Ansible

安裝方法有不少,這裏僅僅以Centos yum安裝爲例。

Ansible默認不在標準倉庫中,須要用到EPEL源。

#yum install epel-release -y 

#yum install ansible -y 

  查看本身安裝的版本

#ansible --version
ansible 2.8.0.dev0
  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, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

Ansible功能詳解

配置文件

配置文件或者指令  描述                    
/etc/ansible/ansible.cfg  主配置件,配置ansible工做特性
/etc/ansible/hosts 主機清單 
/etc/ansible/roles/  存放角色的目錄 
/usr/bin/ansible  主程序,臨時命令執行工具 
/usr/bin/ansible-doc  查看配置文檔,模塊功能查看工具 
/usr/bin/ansible-galaxy 下載/上傳優秀代碼或者Roles模塊的官網平臺 
/usr/bin/ansible-playbook   定製自動化任務,編排劇本工具
/usr/bin/ansible-pull  遠程執行命令的工具 
/usr/bin/ansible-vault  文件加密工具 
/usr/bin/ansible-console  基於console界面與用戶交互的工具 

Ansible的配置文件

Ansible配置文件/etc/ansible/ansible.cfg(通常保持默認)部分參數的講解

[default]

#inventory = /etc/ansible/hosts #主機列表配置

#library = /usr/share/my_modules/ #庫文件存放目錄

#remote_tmp = $HOME/.ansible/tmp #臨時py命令文件存放在遠程主機目錄

#local_tmp = $HOME/.ansible/tmp  #本地的臨時命令執行目錄

#forks = 5   #默認併發數5

#sudo_user = root #默認sudo用戶

#ask_sudo_pass = True #每次執行ansible命令是否詢問ssh密碼

#ask_pass = True #鏈接是提示輸入ssh密碼

#remote_port = 22  #遠程主機的默認端口

#log_pass = /var/log/ansible.log  #日誌文件路徑

#host_key_checking = False #檢查對應服務器上的host_key,建議取消註釋,也就是不會彈出以下消息:

Are you sure you want to continue connecting (yes/no)?

 

 實驗規劃

 
主機名   系統版本 IP地址   功能
ansible CentOS 7.4 10.39.2.38 ansible的主控端
node1 CentOS 7.5 10.39.0.49 服務器











配置ansible主控端的hosts實現解析,或者直接用ip地址

Inventory 主機清單

ansible必須經過Inventory來管理主機。Ansible可同時操做屬於一個組的多臺主機,組和主機之間的關係經過Inventory文件配置

語法格式:

單臺主機

node1                  >  FQDN

10.39.0.49           > IP地址

10.39.0.49:222    >  非標準ssh端口

 

[webservers]        > 定義一個組名

web1       >組內的單臺主機

10.39.0.49    >

 

[dbservers]           

10.39.0.49            >一臺主機能夠是不一樣的組,這臺主機同時屬於[webservers]組

 

[group:children]     > 組嵌套組,group爲自定義的組名,children是關鍵字,固定語法,必須填寫

webservers      > group組內包含其餘的組名

dbservers              > group組內包含其餘的組名

 

[webservers]

node[001:006]      > 有規律的名稱列表

這裏表示至關於:

node001

node002

node003

node004

node005

node006

 

[databases]

db-[a:e]             > 定義字母範圍的簡寫模式

這裏至關於:

db-a

db-b

db-c

db-d

db-e

一下這兩條定義了一臺主機的連接方式,而不是讀取默認的配置設定

localhost         ansible_connection=local

node1             ansible_connection=ssh          ansible_ssh_user=root

最後還有一個隱藏的分組,那就是all,表明所有主機,這個是隱式的,不須要寫出來的

 

Inventory參數說明

 ansible_ssh_host

    將要連接的遠程主機名,與你想要設定的主機的別名不一樣的話,可經過此變量設置

 ansible_ssh_port

    ssh端口號,若是不是默認端口號,經過此變量設置,這種業可使用IP:端口  10.39.2.39:222

 ansible_ssh_user

    默認的ssh用戶名

 ansible_ssh_pass

    ssh密碼(這種方式並不安全,建議使用--ask-pass或者ssh密鑰)

 ansible_sudo_pass

    sudo密碼(這種方式並不安全,建議使用--ask-sudo-pass)

 ansible_sudo_exe

    sudo命令路徑(適用於1.8級以上版本)

 ansible_connection

    與主機的連接類型,好比:local,ssh或者paramiko. Ansible 1.2之前默認使用 paramiko.1.2 之後默認使用 'smart','smart' 方式會根據是否支持 ControlPersist, 來判斷'ssh' 方式是否可行

 ansible_ssh_private_key_file

    ssh使用的私鑰文件,適用於有多個密鑰,而你不想使用ssh代理的狀況 

 ansible_shell_type

    目標系統的shell類型。默認狀況下,命令的執行使用「sh」語句,可設置爲「csh」或者「fish」

 ansible_python_interpreter

   目標主機的 python 路徑.適用於的狀況: 系統中有多個 Python, 或者命令路徑不是"/usr/bin/python",好比 \*BSD, 或者 /usr/bin/python 不是 2.X 版本的 Python.咱們不使用 "/usr/bin/env" 機制,由於這要求遠程用戶的路徑設置正確,且要求 "python" 可執行程序名不可爲 python之外的名字(實際有可能名爲python26).

與 ansible_python_interpreter 的工做方式相同,可設定如 ruby 或 perl 的路徑....

下面用幾個例子更直觀的展現一下:

some_host  ansible_ssh_port=222   ansible_ssh_user=manager

aws_host     ansible_ssh_private_key_file=/home/example/.ssh/aws.pem

freebsd_host     ansible_python_interpreter=/usr/local/bin/python

ruby_module_host   ansible_ruby_interpreter=/usr/bin/ruby.1.9.3

第一條ansible命令

很重要的一點,主機清單必需要先配置,

cat /etc/ansible/hosts 
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

[webservers]
10.39.0.49
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Here's another example of host ranges, this time there are no
# leading 0s:

## db-[99:101]-node.example.com

 

#ssh-copy-id root@10.39.0.49  
#配置單向免密登錄

# ansible webservers -m ping #使用ansible對webservers組內的主機進行ping模塊測試 10.39.0.49 | SUCCESS => { "changed": false, "ping": "pong" }

是否是很簡單呀,Ansible用的指令也不是太多,可使用man ansible和官方網站去查詢。http://docs.ansible.com/ansible/latest/

Ansible經常使用命令語法

ansible <host-pattern> [-m module_name] [options]

指令  匹配規則的主機搶單  -m 模塊名  選項

--version 顯示版本

-a 模塊參數(若是有)

-m module指定模塊,默認爲conmand

-v 詳細過程 -vv -vvv更詳細

--list-hosts顯示主機列表,可簡寫--list

-k --ask-pass提示鏈接密碼,默認Key驗證

-K --ask-become-pass 提示使用sudu密碼

-C --check 檢查,並不執行

-T --timeout=TIMEOUT 執行命令的超時時間,默認10S

-u --user=REMOTE_USER 遠程執行的用戶

-U SUDO_USER --sudo-user 指定sudo用戶

-b --become 代替舊版的sudo切換

ansible-doc:顯示模塊幫助

ansible-doc [options] [module...]

-a 顯示全部模塊的文檔
-l, --list 列出可用模塊
-s, --snippet 顯示指定模塊的簡要說明

例子:#ansible-doc ping

因爲ansible的模塊有1378個(2.4.2.0),而且一直在持續更新。所以,這個指令必需要掌握的。
#ansible-doc -l |wc -l
1378

 

Ansible 主機 匹配列表

通配符

注意用單引號
* 匹配任意字符
#ansible '*' -m ping 等同於 #ansible all -m ping

#ansible '*dns*' -m ping
6-dns-1.hunk.tech | SUCCESS

? 匹配單個字符
#ansible '192.168.7.20?' -m ping
192.168.7.201 | SUCCESS
192.168.7.203 | SUCCESS
192.168.7.202 | SUCCESS
192.168.7.200 | SUCCESS

: 或者
#ansible '192.168.7.201:192.168.7.254' -m ping
192.168.7.201 | SUCCESS
192.168.7.254 | SUCCESS

:& 而且 (邏輯與)
#ansible 'test3:&test' --list

hosts (1):
192.168.7.254

:! 邏輯非。在test3組內,可是並不在test組內
#ansible 'test3:!test' --list > 用到感嘆號的時候,記得引號爲單引號,不然會被bash解析爲歷史命令

hosts (2):
192.168.7.200
192.168.7.203

使用正則表達式
~表示後面是正則匹配,注意~後面不能有空格
#ansible '~[67]-(db|dns).*\.hunk.*' --list

hosts (2):
6-dns-1.hunk.tech
7-db-3.hunk.tech

 

Ansible 的命令執行過程

以 ansible webservers -m command -a 'ls -l /' -vvv 這條命令爲例,根據顯示的信息時行解讀

1.加載本身的配置文件,默認/etc/ansible/ansible.cfg
Using /etc/ansible/ansible.cfg as config file

2.匹配主機清單
Parsed /etc/ansible/hosts inventory source with ini plugin

3. 加載指令對應的模塊文件,如command,生成.py的文件到本機的臨時目錄,這個目錄就是在/etc/ansible/ansible.cfg定義的

Using module file /usr/lib/python2.7/site-packages/ansible/modules/commands/command.py

PUT /root/.ansible/tmp/ansible-local-2495QkKiC4/tmp4T17Dk TO /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/AnsiballZ_command.py

4.經過ansible將模塊或命令生成對應的臨時py文件,並將該文件傳輸至遠程服務器的對應執行用戶$HOME/.ansible/tmp/ansible-tmp-數字/XXX.PY文件,
這個目錄就是在/etc/ansible/ansible.cfg定義的

sftp> put /root/.ansible/tmp/ansible-local-2495QkKiC4/tmp4T17Dk /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/AnsiballZ_command.py\n

5.給文件+x權限

chmod u+x /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/ /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/AnsiballZ_command.py && sleep 0

6.執行並返回結果

/usr/bin/python /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/AnsiballZ_command.py && sleep 0

7.刪除臨時py文件,sleep 0退出

rm -f -r /root/.ansible/tmp/ansible-tmp-1559646714.7-176279961872658/ > /dev/null 2>&1 && sleep 0

8.關閉遠程主機鏈接

Shared connection to 10.39.0.49 closed.\r\n

 

執行結果狀態

 綠色:執行成功而且不須要作改變的操做

 ×××:執行成功而且對目標主機作變動

 紅色:執行失敗

相關文章
相關標籤/搜索