自動化運維工具Ansible之Tests測驗詳解

 

Ansible Tests 詳解與使用案例html

 

主機規劃

 

添加用戶帳號

說明:python

一、 運維人員使用的登陸帳號;web

二、 全部的業務都放在 /app/ 下「yun用戶的家目錄」,避免業務數據亂放;正則表達式

三、 該用戶也被 ansible 使用,由於幾乎全部的生產環境都是禁止 root 遠程登陸的(所以該 yun 用戶也進行了 sudo 提權)。shell

1 # 使用一個專門的用戶,避免直接使用root用戶
2 # 添加用戶、指定家目錄並指定用戶密碼
3 # sudo提權
4 # 讓其它普通用戶能夠進入該目錄查看信息
5 useradd -u 1050 -d /app yun && echo '123456' | /usr/bin/passwd --stdin yun
6 echo "yun  ALL=(ALL)       NOPASSWD: ALL" >>  /etc/sudoers
7 chmod 755 /app/

 

Ansible 配置清單Inventory

以後文章都是以下主機配置清單app

 1 [yun@ansi-manager ansible_info]$ pwd
 2 /app/ansible_info
 3 [yun@ansi-manager ansible_info]$ cat hosts_key 
 4 # 方式一、主機 + 端口 + 密鑰
 5 [manageservers]
 6 172.16.1.180:22
 7 
 8 [proxyservers]
 9 172.16.1.18[1:2]:22
10 
11 # 方式2:別名 + 主機 + 端口 + 密碼
12 [webservers]
13 web01 ansible_ssh_host=172.16.1.183 ansible_ssh_port=22
14 web02 ansible_ssh_host=172.16.1.184 ansible_ssh_port=22
15 web03 ansible_ssh_host=172.16.1.185 ansible_ssh_port=22

 

Tests 概述

Tests 在 Jinja 中是一種評估模板表達式,並最終返回 True 或 False。Jinja 中就有自帶的 Tests 清單,具體地址以下:運維

http://docs.jinkan.org/docs/jinja2/templates.html#builtin-tests

 

tests 和 filters 的主要區別在於Jinja tests 用於比較,而 filters 用於數據操做,二者在Jinja中有不一樣的應用。ssh

與全部模板同樣,tests 老是在 Ansible 控制機上執行,而不是在任務的目標機上,由於它們測驗本地數據。ui

除了 Jinja2 tests 以外,Ansible還提供了一些 tests,用戶也能夠輕鬆建立本身的 tests。this

測驗字符串

若要將字符串與子字符串或正則表達式匹配,請使用「match」、「search」或「regex」過濾。

match:必須有開頭匹配

search:子串匹配

regex:正則匹配

示例:

 1 [yun@ansi-manager ansi_tests]$ pwd
 2 /app/ansible_info/ansi_tests
 3 [yun@ansi-manager ansi_tests]$ cat tests_str.yml 
 4 ---
 5 
 6 - hosts: manageservers
 7   vars:
 8     url: "http://example.com/users/foo/resources/bar"
 9 
10   tasks:
11     - debug:
12         msg: "matched pattern 1-1"
13       when: url is match("http://example.com/users/.*/resources/.*") # True
14 
15     - debug:
16         msg: "matched pattern 1-2"
17       when: url is match("http://example.com") # True
18 
19     - debug:
20         msg: "matched pattern 1-3"
21       when: url is match(".*://example.com") # True
22 
23     - debug:
24         msg: "matched pattern 1-4"
25       when: url is match("example.com/users/.*/resources/.*") # False
26 
27     - debug:
28         msg: "matched pattern 2-1"
29       when: url is search("/users/.*/resources/.*") # True
30 
31     - debug:
32         msg: "matched pattern 2-2"
33       when: url is search("/users/") # True
34 
35     - debug:
36         msg: "matched pattern 2-3"
37       when: url is search("/user/") # False
38 
39     - debug:
40         msg: "matched pattern 3"
41       when: url is regex("example.com/\w+/foo") # True
42 
43 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_str.yml  # 注意查看執行

 

測驗版本比較

使用「version」,用於版本號比較。

「version」接受的運算符以下:

<, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne

 

「version」也能夠接受「strict」參數,這個參數默認值爲「False」,若是設置爲「True」則ansible會進行更嚴格的版本檢查:

{{ sample_version_var is version('1.0', operator='lt', strict=True) }}

 

示例:

 1 # 判斷 ansible_python_version 版本是否 大於等於 2.7.3
 2 [yun@ansi-manager ansi_tests]$ pwd
 3 /app/ansible_info/ansi_tests
 4 [yun@ansi-manager ansi_tests]$ ll
 5 total 8
 6 drwxrwxr-x 2 yun yun  35 Sep 12 15:14 file
 7 -rw-rw-r-- 1 yun yun 209 Sep 12 15:08 tests_version.yml
 8 [yun@ansi-manager ansi_tests]$ cat file/tests_version.conf.j2   # 涉及文件
 9 # Jinja2 版本測驗
10 
11 {% if ansible_python_version is version('2.7.3', '>=') %}
12 result True. ansible_python_version = {{ ansible_python_version }}
13 {% else %}
14 result False
15 {% endif %}
16 
17 [yun@ansi-manager ansi_tests]$ cat tests_version.yml  # 涉及的playbook文件
18 ---
19 # ansible tests Version Comparison
20 
21 - hosts: proxyservers
22 
23   tasks:
24     - name: "Tests Version Comparison"
25       template:
26         src: ./file/tests_version.conf.j2
27         dest: /tmp/tests_version.conf
28 
29 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_version.yml   # 執行

 

測驗子集和超集

關鍵字「superset」和「subset」,用於測驗一個列表是否包含被包含於另外一個列表

示例:

 1 [yun@ansi-manager ansi_tests]$ pwd
 2 /app/ansible_info/ansi_tests
 3 [yun@ansi-manager ansi_tests]$ cat tests_set.yml 
 4 ---
 5 # tests 子集和超集
 6 - hosts: manageservers
 7 
 8   vars:
 9     a: [1,2,3,4,5]
10     b: [2,3]
11   tasks:
12     - debug:
13         msg: "A includes B"
14       when: a is superset(b)
15 
16     - debug:
17         msg: "B is included in A"
18       when: b is subset(a)
19 
20 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_set.yml  # 注意查看執行

 

測驗列表真假

關鍵字「all」和「any」,用於檢查列表裏元素的真假,列表中全部爲真或者任何一個爲真。

all:一假則假

any:一真則真

 

 1 [yun@ansi-manager ansi_tests]$ pwd
 2 /app/ansible_info/ansi_tests
 3 [yun@ansi-manager ansi_tests]$ cat tests_list.yml 
 4 ---
 5 #  tests 測驗 all any
 6 - hosts: manageservers
 7 
 8   vars:
 9     mylist:
10       - 1
11       - "{{ 3 == 3 }}"
12       - True
13     myotherlist:
14       - False
15       - True
16 
17   tasks:
18     - debug:
19         msg: "all are true!"
20       when: mylist is all
21 
22     - debug:
23         msg: "at least one is true"
24       when: myotherlist is any
25 
26 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_list.yml  # 注意查看執行

  

測驗文件或目錄

用於測驗目錄、文件、軟鏈接、是否已存在、是相對路徑仍是絕對路徑等等。

 1 [yun@ansi-manager ansi_tests]$ pwd
 2 /app/ansible_info/ansi_tests
 3 [yun@ansi-manager ansi_tests]$ cat tests_path.yml 
 4 ---
 5 - hosts: manageservers
 6 
 7   vars:
 8     # - mypath: /tmp/
 9     - mypath: /tmp/yum_hard.sh
10     - path2: /tmp/
11     # - path2: /tmp/yum_hard_2.sh
12 
13   tasks:
14     - debug:
15         msg: "path is a directory"
16       when: mypath is directory
17 
18     - debug:
19         msg: "path is a file"
20       when: mypath is file
21 
22     - debug:
23         msg: "path is a symlink"
24       when: mypath is link
25 
26     - debug:
27         msg: "path already exists"
28       when: mypath is exists
29 
30     - debug:
31         msg: "path is {{ (mypath is abs)|ternary('absolute','relative')}}"
32 
33     - debug:
34         msg: "path is the same file as path2"
35       when: mypath is same_file(path2)
36 
37     - debug:
38         msg: "path is a mount"
39       when: mypath is mount
40 
41 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_path.yml  # 注意查看執行

 

測驗任務執行結果

對任務執行結果進行測驗。

 1 [yun@ansi-manager ansi_tests]$ pwd
 2 /app/ansible_info/ansi_tests
 3 [yun@ansi-manager ansi_tests]$ cat tests_result.yml 
 4 ---
 5 - hosts: 172.16.1.180
 6 
 7   ## 對以下3種狀況一次測驗
 8   tasks:
 9     - shell: /usr/bin/foo
10     #- shell: /usr/bin/true
11     #- shell: /usr/bin/false
12       register: result
13       ignore_errors: True
14 
15     - debug:
16         msg: "{{ result }}"
17 
18     - debug:
19         msg: "it failed"
20       when: result is failed
21 
22     # in most cases you'll want a handler, but if you want to do something right now, this is nice
23     - debug:
24         msg: "it changed"
25       when: result is changed
26 
27     - debug:
28         msg: "it succeeded in Ansible >= 2.1"
29       when: result is succeeded
30 
31     - debug:
32         msg: "it succeeded"
33       when: result is success
34 
35     - debug:
36         msg: "it was skipped"
37       when: result is skipped
38 
39 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_result.yml  # 注意查看執行

 

 


 

———END———
若是以爲不錯就關注下唄 (-^O^-) !

相關文章
相關標籤/搜索