json yaml playbook語法

Top

NSD ARCHITECTURE DAY02

  1. 練習1:playbook練習
  2. 案例2:變量練習
  3. 案例3:handlers練習
  4. 案例4:編寫playbook

1 練習1:playbook練習

1.1 問題

本案例要求:html

  • 安裝Apache並修改監聽端口爲8080
  • 修改ServerName配置,執行apachectl -t命令不報錯
  • 設置默認主頁hello world
  • 啓動服務並設開機自啓

1.2 步驟

實現此案例須要按照以下步驟進行。web

步驟一:playbook的ping腳本檢測shell

  1. [root@ansible ansible]# vim ping.yml
  2. ---
  3. - hosts: all
  4. remote_user: root
  5. tasks:
  6. - ping:
  7. [root@ansible ansible]# ansible-playbook ping.yml //輸出結果
  8. PLAY [all] *******************************************************************
  9. TASK [Gathering Facts] *******************************************************
  10. ok: [web1]
  11. ok: [web2]
  12. ok: [cache]
  13. ok: [db1]
  14. ok: [db2]
  15. TASK [ping] ******************************************************************
  16. ok: [db1]
  17. ok: [web2]
  18. ok: [cache]
  19. ok: [web1]
  20. ok: [db2]
  21. PLAY RECAP *******************************************************************
  22. cache : ok=2 changed=0 unreachable=0 failed=0
  23. db1 : ok=2 changed=0 unreachable=0 failed=0
  24. db2 : ok=2 changed=0 unreachable=0 failed=0
  25. web1 : ok=2 changed=0 unreachable=0 failed=0
  26. web2 : ok=2 changed=0 unreachable=0 failed=0

注意:若是檢測的時候出錯,會在當前的目錄生成一個新的文件(以.retry結尾),能夠去這個文件裏面看是哪一個主機的錯apache

步驟二:用playbook安裝Apache,修改端口,配置ServerName,修改主頁,設置開機自啓vim

  1. [root@ansible ansible]# vim http.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - name: install one specific version of Apache
  7. yum:
  8. name: httpd        //安裝Apache
  9. state: installed
  10. - lineinfile:
  11. path: /etc/httpd/conf/httpd.conf
  12. regexp: '^Listen '
  13. line: 'Listen 8080'        //修改端口爲8080
  14. - replace:
  15. path: /etc/httpd/conf/httpd.conf
  16. regexp: '^#(ServerName).*'        //配置ServerName
  17. replace: '\1 localhost'
  18. - service:
  19. name: httpd
  20. enabled: yes        //開機自啓
  21. state: restarted
  22. - copy:
  23. src: /root/index.html        //修改主頁,能夠本身寫個頁面
  24. dest: /var/www/html/index.html
  25. [root@ansible ansible]# curl 192.168.1.56:8080
  26. hello world
  27. [root@ansible ansible]# ssh cache
  28. Last login: Fri Sep 7 09:32:05 2018 from 192.168.1.51
  29. [root@cache ~]# apachectl -t
  30. Syntax OK

2 案例2:變量練習

2.1 問題

本案例要求熟悉playbook進階:ssh

  • 練習使用user模塊添加用戶
  • 練習使用變量簡化task,讓play通用性更強
  • 練習使用過濾器

2.2 步驟

實現此案例須要按照以下步驟進行。curl

步驟一:使用user模塊添加用戶,並修改密碼測試

  1. [root@ansible ansible]# vim user.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. username: xiaoming
  7. tasks:
  8. - name: create user "{{username}}"
  9. user: group=wheel uid=1000 name={{username}}
  10. - shell: echo 123456 | passwd --stdin xiaoming
  11. - shell: chage -d 0 {{username}}
  12. [root@ansible ansible]# ansible-playbook user.yml //執行結果
  13. PLAY [cache] ******************************************************************
  14. TASK [Gathering Facts] ********************************************************
  15. ok: [cache]
  16. TASK [create user " xiaoming "] ***********************************************
  17. changed: [cache]
  18. TASK [command] ****************************************************************
  19. changed: [cache]
  20. TASK [command] ****************************************************************
  21. changed: [cache]
  22. PLAY RECAP ********************************************************************
  23. cache : ok=4 changed=3 unreachable=0 failed=0

步驟二:變量過濾器,建立一個用戶,設置密碼ui

  1. [root@ansible ansible]# vim user1.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - user:
  7. name: lisi
  8. group: root
  9. password: "{{'123456' | password_hash('sha512')}}"
  10. - shell: chage -d 0 lisi
  11. [root@ansible ansible]# ansible-playbook user1.yml
  12. PLAY [cache] ******************************************************************
  13. TASK [Gathering Facts] ********************************************************
  14. ok: [cache]
  15. TASK [user] *******************************************************************
  16. changed: [cache]
  17. TASK [command] ****************************************************************
  18. changed: [cache]
  19. PLAY RECAP ********************************************************************
  20. cache : ok=3 changed=2 unreachable=0 failed=0

步驟三:定義一個變量建立用戶url

  1. [root@ansible ansible]# vim user2.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. user: zhangs
  7. tasks:
  8. - user:
  9. name: "{{user}}"
  10. group: root
  11. password: "{{'123456' | password_hash('sha512')}}"
  12. - shell: chage -d 0 "{{user}}"
  13. [root@ansible ansible]# ansible-playbook user2.yml
  14. PLAY [cache] ******************************************************************
  15. TASK [Gathering Facts] ********************************************************
  16. ok: [cache]
  17. TASK [user] *******************************************************************
  18. changed: [cache]
  19. TASK [command] ****************************************************************
  20. changed: [cache]
  21. PLAY RECAP ********************************************************************
  22. cache : ok=3 changed=2 unreachable=0 failed=0

3 案例3:handlers練習

3.1 問題

本案例要求:

  • 安裝Apache軟件
  • 配置文件,從新載入配置文件讓服務生效
  • 使用handlers來實現

3.2 步驟

實現此案例須要按照以下步驟進行。

步驟一:error

playbook從上往下順序執行,若報錯,後面的命令不會在執行,若想解決有兩種方法:

1)當返回值爲假時,顯示true: - shell: setenforce 0 || true

  1. [root@ansible ansible]# vim user5.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. user: bb
  7. tasks:
  8. - shell: setenforce 0 || true
  9. - user:
  10. name: "{{user}}"
  11. group: root
  12. password: "{{'123456' | password_hash('sha512')}}"
  13. - shell: chage -d 0 "{{user}}"
  14. [root@ansible ansible]# ansible-playbook user5.yml
  15. PLAY [cache] ******************************************************************
  16. TASK [Gathering Facts] ********************************************************
  17. ok: [cache]
  18. TASK [command] ****************************************************************
  19. changed: [cache]
  20. TASK [user] *******************************************************************
  21. changed: [cache]
  22. TASK [command] ****************************************************************
  23. changed: [cache]
  24. PLAY RECAP ********************************************************************
  25. cache : ok=4 changed=3 unreachable=0 failed=0

二、忽略:ignoring_errors: True(推薦使用這個,會有報錯信息,告訴你錯誤忽略,繼續執行下面的命令)

  1. [root@ansible ansible]# vim user6.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. user: bb
  7. tasks:
  8. - shell: setenforce 0
  9. ignore_errors: True
  10. - user:
  11. name: "{{user}}"
  12. group: root
  13. password: "{{'123456' | password_hash('sha512')}}"
  14. - shell: chage -d 0 "{{user}}"
  15. [root@ansible ansible]# ansible-playbook user6.yml
  16. PLAY [cache] ******************************************************************
  17. TASK [Gathering Facts] ********************************************************
  18. ok: [cache]
  19. TASK [command] ****************************************************************
  20. fatal: [cache]: FAILED! => {"changed": true, "cmd": "setenforce 0", "delta": "0:00:00.004198", "end": "2018-09-07 11:08:14.936959", "msg": "non-zero return code", "rc": 1, "start": "2018-09-07 11:08:14.932761", "stderr": "setenforce: SELinux is disabled", "stderr_lines": ["setenforce: SELinux is disabled"], "stdout": "", "stdout_lines": []}
  21. ...ignoring
  22. TASK [user] *******************************************************************
  23. changed: [cache]
  24. TASK [command] ****************************************************************
  25. changed: [cache]
  26. PLAY RECAP ********************************************************************
  27. cache : ok=4 changed=3 unreachable=0 failed=0

步驟二: handlers

關注的資源發生變化時採起的操做

1) 使用handlers來配置文件,從新載入配置文件讓服務生效

  1. [root@ansible ansible]# vim adhttp.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - copy:
  7. src: /root/httpd.conf
  8. dest: /etc/httpd/conf/httpd.conf
  9. owner: root
  10. group: root
  11. mode: 0644
  12. notify:
  13. - restart httpd
  14. handlers:
  15. - name: restart httpd
  16. service: name=httpd state=restarted
  17. [root@ansible ansible]# ansible-playbook adhttp.yml
  18. PLAY [cache] ******************************************************************
  19. TASK [Gathering Facts] ********************************************************
  20. ok: [cache]
  21. TASK [copy] *******************************************************************
  22. ok: [cache]
  23. PLAY RECAP ********************************************************************
  24. cache : ok=2 changed=0 unreachable=0 failed=0
  25. [root@ansible ansible]# ssh cache apachectl -t
  26. Syntax OK
  27. [root@ansible ansible]# curl 192.168.1.56:8080
  28. hello world

2)使用腳本調用變量更改服務

  1. [root@ansible ansible]# vim adhttp2.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. vars:
  6. server: httpd
  7. tasks:
  8. - copy:
  9. src: /root/httpd.conf
  10. dest: /etc/httpd/conf/httpd.conf
  11. owner: root
  12. group: root
  13. mode: 0644
  14. notify:
  15. - restart "{{server}}"
  16. handlers:
  17. - name: restart "{{server}}"
  18. service: name=httpd state=restarted
  19. [root@ansible ansible]# ansible-playbook adhttp2.yml
  20. PLAY [cache] ************************************************************************************************************
  21. TASK [Gathering Facts] **************************************************************************************************
  22. ok: [cache]
  23. TASK [copy] *************************************************************************************************************
  24. ok: [cache]
  25. PLAY RECAP **************************************************************************************************************
  26. cache : ok=2 changed=0 unreachable=0 failed=0
  27. [root@ansible ansible]#

4 案例4:編寫playbook

4.1 問題

本案例要求:

  • 把全部監聽端口是8080的Apache服務所有中止

4.2 步驟

實現此案例須要按照以下步驟進行。

步驟一:把監聽端口是8080的Apache服務所有中止

  1. [root@ansible ansible]# vim ad.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - shell: netstat -atunlp | awk '{print $4}'| awk '-F:' '{print $2}'
  7. register: result
  8. - service:
  9. name: httpd
  10. state: stopped
  11. [root@ansible ansible]# ansible-playbook ad.yml
  12. PLAY [cache] ************************************************************************************************************
  13. TASK [Gathering Facts] **************************************************************************************************
  14. ok: [cache]
  15. TASK [command] **********************************************************************************************************
  16. changed: [cache]
  17. TASK [service] **********************************************************************************************************
  18. changed: [cache]
  19. PLAY RECAP **************************************************************************************************************
  20. cache : ok=3 changed=2 unreachable=0 failed=0

步驟二:when條件判斷

1)當系統負載超過0.7時,則關掉httpd

  1. [root@ansible ansible]# vim when.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - shell: uptime | awk '{printf("%.2f",$(NF-2))}'
  7. register: result
  8. - service:
  9. name: httpd
  10. state: stopped
  11. when: result.stdout|float > 0.7
  12. [root@ansible ansible]# ansible-playbook when.yml
  13. PLAY [cache] ************************************************************************************************************
  14. TASK [Gathering Facts] **************************************************************************************************
  15. ok: [cache]
  16. TASK [command] **********************************************************************************************************
  17. changed: [cache]
  18. TASK [service] **********************************************************************************************************
  19. changed: [cache]
  20. PLAY RECAP **************************************************************************************************************
  21. cache : ok=3 changed=2 unreachable=0 failed=0

步驟三:with_items標準循環

1)爲不一樣用戶定義不一樣組

  1. [root@ansible ansible]# vim add.yml
  2. ---
  3. - hosts: web2
  4. remote_user: root
  5. tasks:
  6. - user:
  7. name: "{{item.name}}"
  8. group: "{{item.group}}"
  9. password: "{{'123456'|password_hash('sha512')}}"
  10. with_items:
  11. - {name: "aa", group: "users"}
  12. - {name: "bb", group: "mail" }
  13. - {name: "cc", group: "wheel"}
  14. - {name: "dd", group: "root" }
  15. [root@ansible ansible]# ansible-playbook add.yml
  16. PLAY [web2] *************************************************************************************************************
  17. TASK [Gathering Facts] **************************************************************************************************
  18. ok: [web2]
  19. TASK [user] *************************************************************************************************************
  20. changed: [web2] => (item={u'group': u'users', u'name': u'aa'})
  21. changed: [web2] => (item={u'group': u'mail', u'name': u'bb'})
  22. changed: [web2] => (item={u'group': u'wheel', u'name': u'cc'})
  23. changed: [web2] => (item={u'group': u'root', u'name': u'dd'})
  24. PLAY RECAP **************************************************************************************************************
  25. web2 : ok=2 changed=1 unreachable=0 failed=0

2)嵌套循環,循環添加多用戶

  1. [root@ansible ansible]# vim add1.yml
  2. ---
  3. - hosts: web2
  4. remote_user: root
  5. vars:
  6. un: [a, b, c]
  7. id: [1, 2, 3]
  8. tasks:
  9. - name: add users
  10. shell: echo {{item}}
  11. with_nested:
  12. - "{{un}}"
  13. - "{{id}}"
  14. [root@ansible ansible]# ansible-playbook add1.yml
  15. PLAY [web2] *************************************************************************************************************
  16. TASK [Gathering Facts] **************************************************************************************************
  17. ok: [web2]
  18. TASK [add users] ********************************************************************************************************
  19. changed: [web2] => (item=[u'a', 1])
  20. changed: [web2] => (item=[u'a', 2])
  21. changed: [web2] => (item=[u'a', 3])
  22. changed: [web2] => (item=[u'b', 1])
  23. changed: [web2] => (item=[u'b', 2])
  24. changed: [web2] => (item=[u'b', 3])
  25. changed: [web2] => (item=[u'c', 1])
  26. changed: [web2] => (item=[u'c', 2])
  27. changed: [web2] => (item=[u'c', 3])
  28. PLAY RECAP **************************************************************************************************************
  29. web2 : ok=2 changed=1 unreachable=0 failed=0

步驟四:tags給指定的任務定義一個調用標識

1)tags 樣例

  1. [root@ansible ansible]# vim adhttp.yml
  2. ---
  3. - hosts: cache
  4. remote_user: root
  5. tasks:
  6. - copy:
  7. src: /root/httpd.conf
  8. dest: /etc/httpd/conf/httpd.conf
  9. owner: root
  10. group: root
  11. mode: 0644
  12. tags: config_httpd
  13. notify:
  14. - restart httpd
  15. handlers:
  16. - name: restart httpd
  17. service: name=httpd state=restarted

2)調用方式

  1. [root@ansible ansible]# ansible-playbook adhttp.yml --tags=config_httpd
  2. PLAY [cache] *****************************************************************
  3. TASK [Gathering Facts] *******************************************************
  4. ok: [cache]
  5. TASK [copy] ******************************************************************
  6. ok: [cache]
  7. PLAY RECAP *******************************************************************
  8. cache : ok=2 changed=0 unreachable=0 failed=0

3)include and roles

在編寫playbook的時候隨着項目愈來愈大,playbook愈來愈複雜。能夠把一些play、task 或 handler放到其餘文件中,經過包含進來是一個不錯的選擇

roles像是增強版的include,它能夠引入一個項目的文件和目錄

通常所需的目錄層級有

vars:變量層

tasks:任務層

handlers:觸發條件

files:文件

template:模板

default:默認,優先級最低

  1. ...
  2. tasks:
  3. - include: tasks/setup.yml
  4. - include: tasks/users.yml user=plj
  5. //users.yml 中能夠經過{{ user }}來使用這些變量
  6. handlers:
  7. - include: handlers/handlers.yml

步驟五:debug檢測

  1. [root@ansible ansible]# ansible-playbook --syntax-check http.yml //檢測語法
  2. playbook: http.yml
  3. [root@ansible ansible]# ansible-playbook -C http.yml //測試運行
  4. [root@ansible ansible]# ansible-playbook http.yml --list-tasks
  5. //顯示要執行的工做
  6. playbook: http.yml
  7. play #1 (cache): cache    TAGS: []
  8. tasks:
  9. install one specific version of Apache    TAGS: []
  10. lineinfile    TAGS: []
  11. replace    TAGS: []
  12. service    TAGS: []
  13. copy    TAGS: []
  14. [root@ansible ansible]# vim debug.yml
  15. ---
  16. - hosts: cache
  17. remote_user: root
  18. tasks:
  19. - shell: uptime |awk '{printf("%f\n",$(NF-2))}'
  20. register: result
  21. - shell: touch /tmp/isreboot
  22. when: result.stdout|float > 0.5
  23. - name: Show debug info
  24. debug: var=result
  25. [root@ansible ansible]# ansible-playbook debug.yml         //運行
  26. PLAY [cache] ************************************************************************************************************
  27. TASK [Gathering Facts] **************************************************************************************************
  28. ok: [cache]
  29. TASK [command] **********************************************************************************************************
  30. changed: [cache]
  31. TASK [command] **********************************************************************************************************
  32. skipping: [cache]
  33. TASK [Show debug info] **************************************************************************************************
  34. ok: [cache] => {
  35. "result": {
  36. "changed": true,
  37. "cmd": "uptime |awk '{printf(\"%f\\n\",$(NF-2))}'",
  38. "delta": "0:00:00.005905",
  39. "end": "2018-09-07 12:57:51.371013",
  40. "failed": false,
  41. "rc": 0,
  42. "start": "2018-09-07 12:57:51.365108",
  43. "stderr": "",
  44. "stderr_lines": [],
  45. "stdout": "0.000000",
  46. "stdout_lines": [
  47. "0.000000"
  48. ]
  49. }
  50. }
  51. PLAY RECAP **************************************************************************************************************
  52. cache : ok=3 changed=1 unreachable=0 failed=0
相關文章
相關標籤/搜索