Top
NSD ARCHITECTURE DAY02
- 練習1:playbook練習
- 案例2:變量練習
- 案例3:handlers練習
- 案例4:編寫playbook
1 練習1:playbook練習
1.1 問題
本案例要求:html
- 安裝Apache並修改監聽端口爲8080
- 修改ServerName配置,執行apachectl -t命令不報錯
- 設置默認主頁hello world
- 啓動服務並設開機自啓
1.2 步驟
實現此案例須要按照以下步驟進行。web
步驟一:playbook的ping腳本檢測shell
- [root@ansible ansible]# vim ping.yml
- ---
- - hosts: all
- remote_user: root
- tasks:
- - ping:
- [root@ansible ansible]# ansible-playbook ping.yml
- PLAY [all] *******************************************************************
- TASK [Gathering Facts] *******************************************************
- ok: [web1]
- ok: [web2]
- ok: [cache]
- ok: [db1]
- ok: [db2]
- TASK [ping] ******************************************************************
- ok: [db1]
- ok: [web2]
- ok: [cache]
- ok: [web1]
- ok: [db2]
- PLAY RECAP *******************************************************************
- cache : ok=2 changed=0 unreachable=0 failed=0
- db1 : ok=2 changed=0 unreachable=0 failed=0
- db2 : ok=2 changed=0 unreachable=0 failed=0
- web1 : ok=2 changed=0 unreachable=0 failed=0
- web2 : ok=2 changed=0 unreachable=0 failed=0
注意:若是檢測的時候出錯,會在當前的目錄生成一個新的文件(以.retry結尾),能夠去這個文件裏面看是哪一個主機的錯apache
步驟二:用playbook安裝Apache,修改端口,配置ServerName,修改主頁,設置開機自啓vim
- [root@ansible ansible]# vim http.yml
- ---
- - hosts: cache
- remote_user: root
- tasks:
- - name: install one specific version of Apache
- yum:
- name: httpd
- state: installed
- - lineinfile:
- path: /etc/httpd/conf/httpd.conf
- regexp: '^Listen '
- line: 'Listen 8080'
- - replace:
- path: /etc/httpd/conf/httpd.conf
- regexp: '^#(ServerName).*'
- replace: '\1 localhost'
- - service:
- name: httpd
- enabled: yes
- state: restarted
- - copy:
- src: /root/index.html
- dest: /var/www/html/index.html
- [root@ansible ansible]# curl 192.168.1.56:8080
- hello world
- [root@ansible ansible]# ssh cache
- Last login: Fri Sep 7 09:32:05 2018 from 192.168.1.51
- [root@cache ~]# apachectl -t
- Syntax OK
2 案例2:變量練習
2.1 問題
本案例要求熟悉playbook進階:ssh
- 練習使用user模塊添加用戶
- 練習使用變量簡化task,讓play通用性更強
- 練習使用過濾器
2.2 步驟
實現此案例須要按照以下步驟進行。curl
步驟一:使用user模塊添加用戶,並修改密碼測試
- [root@ansible ansible]# vim user.yml
- ---
- - hosts: cache
- remote_user: root
- vars:
- username: xiaoming
- tasks:
- - name: create user "{{username}}"
- user: group=wheel uid=1000 name={{username}}
- - shell: echo 123456 | passwd --stdin xiaoming
- - shell: chage -d 0 {{username}}
- [root@ansible ansible]# ansible-playbook user.yml
- PLAY [cache] ******************************************************************
- TASK [Gathering Facts] ********************************************************
- ok: [cache]
- TASK [create user " xiaoming "] ***********************************************
- changed: [cache]
- TASK [command] ****************************************************************
- changed: [cache]
- TASK [command] ****************************************************************
- changed: [cache]
- PLAY RECAP ********************************************************************
- cache : ok=4 changed=3 unreachable=0 failed=0
步驟二:變量過濾器,建立一個用戶,設置密碼ui
- [root@ansible ansible]# vim user1.yml
- ---
- - hosts: cache
- remote_user: root
- tasks:
- - user:
- name: lisi
- group: root
- password: "{{'123456' | password_hash('sha512')}}"
- - shell: chage -d 0 lisi
- [root@ansible ansible]# ansible-playbook user1.yml
- PLAY [cache] ******************************************************************
- TASK [Gathering Facts] ********************************************************
- ok: [cache]
- TASK [user] *******************************************************************
- changed: [cache]
- TASK [command] ****************************************************************
- changed: [cache]
- PLAY RECAP ********************************************************************
- cache : ok=3 changed=2 unreachable=0 failed=0
步驟三:定義一個變量建立用戶url
- [root@ansible ansible]# vim user2.yml
- ---
- - hosts: cache
- remote_user: root
- vars:
- user: zhangs
- tasks:
- - user:
- name: "{{user}}"
- group: root
- password: "{{'123456' | password_hash('sha512')}}"
- - shell: chage -d 0 "{{user}}"
- [root@ansible ansible]# ansible-playbook user2.yml
- PLAY [cache] ******************************************************************
- TASK [Gathering Facts] ********************************************************
- ok: [cache]
- TASK [user] *******************************************************************
- changed: [cache]
- TASK [command] ****************************************************************
- changed: [cache]
- PLAY RECAP ********************************************************************
- 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
- [root@ansible ansible]# vim user5.yml
- ---
- - hosts: cache
- remote_user: root
- vars:
- user: bb
- tasks:
- - shell: setenforce 0 || true
- - user:
- name: "{{user}}"
- group: root
- password: "{{'123456' | password_hash('sha512')}}"
- - shell: chage -d 0 "{{user}}"
- [root@ansible ansible]# ansible-playbook user5.yml
- PLAY [cache] ******************************************************************
- TASK [Gathering Facts] ********************************************************
- ok: [cache]
- TASK [command] ****************************************************************
- changed: [cache]
- TASK [user] *******************************************************************
- changed: [cache]
- TASK [command] ****************************************************************
- changed: [cache]
- PLAY RECAP ********************************************************************
- cache : ok=4 changed=3 unreachable=0 failed=0
二、忽略:ignoring_errors: True(推薦使用這個,會有報錯信息,告訴你錯誤忽略,繼續執行下面的命令)
- [root@ansible ansible]# vim user6.yml
- ---
- - hosts: cache
- remote_user: root
- vars:
- user: bb
- tasks:
- - shell: setenforce 0
- ignore_errors: True
- - user:
- name: "{{user}}"
- group: root
- password: "{{'123456' | password_hash('sha512')}}"
- - shell: chage -d 0 "{{user}}"
- [root@ansible ansible]# ansible-playbook user6.yml
- PLAY [cache] ******************************************************************
- TASK [Gathering Facts] ********************************************************
- ok: [cache]
- TASK [command] ****************************************************************
- 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": []}
- ...ignoring
- TASK [user] *******************************************************************
- changed: [cache]
- TASK [command] ****************************************************************
- changed: [cache]
- PLAY RECAP ********************************************************************
- cache : ok=4 changed=3 unreachable=0 failed=0
步驟二: handlers
關注的資源發生變化時採起的操做
1) 使用handlers來配置文件,從新載入配置文件讓服務生效
- [root@ansible ansible]# vim adhttp.yml
- ---
- - hosts: cache
- remote_user: root
- tasks:
- - copy:
- src: /root/httpd.conf
- dest: /etc/httpd/conf/httpd.conf
- owner: root
- group: root
- mode: 0644
- notify:
- - restart httpd
- handlers:
- - name: restart httpd
- service: name=httpd state=restarted
- [root@ansible ansible]# ansible-playbook adhttp.yml
- PLAY [cache] ******************************************************************
- TASK [Gathering Facts] ********************************************************
- ok: [cache]
- TASK [copy] *******************************************************************
- ok: [cache]
- PLAY RECAP ********************************************************************
- cache : ok=2 changed=0 unreachable=0 failed=0
- [root@ansible ansible]# ssh cache apachectl -t
- Syntax OK
- [root@ansible ansible]# curl 192.168.1.56:8080
- hello world
2)使用腳本調用變量更改服務
- [root@ansible ansible]# vim adhttp2.yml
- ---
- - hosts: cache
- remote_user: root
- vars:
- server: httpd
- tasks:
- - copy:
- src: /root/httpd.conf
- dest: /etc/httpd/conf/httpd.conf
- owner: root
- group: root
- mode: 0644
- notify:
- - restart "{{server}}"
- handlers:
- - name: restart "{{server}}"
- service: name=httpd state=restarted
- [root@ansible ansible]# ansible-playbook adhttp2.yml
- PLAY [cache] ************************************************************************************************************
- TASK [Gathering Facts] **************************************************************************************************
- ok: [cache]
- TASK [copy] *************************************************************************************************************
- ok: [cache]
- PLAY RECAP **************************************************************************************************************
- cache : ok=2 changed=0 unreachable=0 failed=0
- [root@ansible ansible]#
4 案例4:編寫playbook
4.1 問題
本案例要求:
- 把全部監聽端口是8080的Apache服務所有中止
4.2 步驟
實現此案例須要按照以下步驟進行。
步驟一:把監聽端口是8080的Apache服務所有中止
- [root@ansible ansible]# vim ad.yml
- ---
- - hosts: cache
- remote_user: root
- tasks:
- - shell: netstat -atunlp | awk '{print $4}'| awk '-F:' '{print $2}'
- register: result
- - service:
- name: httpd
- state: stopped
- [root@ansible ansible]# ansible-playbook ad.yml
- PLAY [cache] ************************************************************************************************************
- TASK [Gathering Facts] **************************************************************************************************
- ok: [cache]
- TASK [command] **********************************************************************************************************
- changed: [cache]
- TASK [service] **********************************************************************************************************
- changed: [cache]
- PLAY RECAP **************************************************************************************************************
- cache : ok=3 changed=2 unreachable=0 failed=0
步驟二:when條件判斷
1)當系統負載超過0.7時,則關掉httpd
- [root@ansible ansible]# vim when.yml
- ---
- - hosts: cache
- remote_user: root
- tasks:
- - shell: uptime | awk '{printf("%.2f",$(NF-2))}'
- register: result
- - service:
- name: httpd
- state: stopped
- when: result.stdout|float > 0.7
- [root@ansible ansible]# ansible-playbook when.yml
- PLAY [cache] ************************************************************************************************************
- TASK [Gathering Facts] **************************************************************************************************
- ok: [cache]
- TASK [command] **********************************************************************************************************
- changed: [cache]
- TASK [service] **********************************************************************************************************
- changed: [cache]
- PLAY RECAP **************************************************************************************************************
- cache : ok=3 changed=2 unreachable=0 failed=0
步驟三:with_items標準循環
1)爲不一樣用戶定義不一樣組
- [root@ansible ansible]# vim add.yml
- ---
- - hosts: web2
- remote_user: root
- tasks:
- - user:
- name: "{{item.name}}"
- group: "{{item.group}}"
- password: "{{'123456'|password_hash('sha512')}}"
- with_items:
- - {name: "aa", group: "users"}
- - {name: "bb", group: "mail" }
- - {name: "cc", group: "wheel"}
- - {name: "dd", group: "root" }
- [root@ansible ansible]# ansible-playbook add.yml
- PLAY [web2] *************************************************************************************************************
- TASK [Gathering Facts] **************************************************************************************************
- ok: [web2]
- TASK [user] *************************************************************************************************************
- changed: [web2] => (item={u'group': u'users', u'name': u'aa'})
- changed: [web2] => (item={u'group': u'mail', u'name': u'bb'})
- changed: [web2] => (item={u'group': u'wheel', u'name': u'cc'})
- changed: [web2] => (item={u'group': u'root', u'name': u'dd'})
- PLAY RECAP **************************************************************************************************************
- web2 : ok=2 changed=1 unreachable=0 failed=0
2)嵌套循環,循環添加多用戶
- [root@ansible ansible]# vim add1.yml
- ---
- - hosts: web2
- remote_user: root
- vars:
- un: [a, b, c]
- id: [1, 2, 3]
- tasks:
- - name: add users
- shell: echo {{item}}
- with_nested:
- - "{{un}}"
- - "{{id}}"
- [root@ansible ansible]# ansible-playbook add1.yml
- PLAY [web2] *************************************************************************************************************
- TASK [Gathering Facts] **************************************************************************************************
- ok: [web2]
- TASK [add users] ********************************************************************************************************
- changed: [web2] => (item=[u'a', 1])
- changed: [web2] => (item=[u'a', 2])
- changed: [web2] => (item=[u'a', 3])
- changed: [web2] => (item=[u'b', 1])
- changed: [web2] => (item=[u'b', 2])
- changed: [web2] => (item=[u'b', 3])
- changed: [web2] => (item=[u'c', 1])
- changed: [web2] => (item=[u'c', 2])
- changed: [web2] => (item=[u'c', 3])
- PLAY RECAP **************************************************************************************************************
- web2 : ok=2 changed=1 unreachable=0 failed=0
步驟四:tags給指定的任務定義一個調用標識
1)tags 樣例
- [root@ansible ansible]# vim adhttp.yml
- ---
- - hosts: cache
- remote_user: root
- tasks:
- - copy:
- src: /root/httpd.conf
- dest: /etc/httpd/conf/httpd.conf
- owner: root
- group: root
- mode: 0644
- tags: config_httpd
- notify:
- - restart httpd
- handlers:
- - name: restart httpd
- service: name=httpd state=restarted
2)調用方式
- [root@ansible ansible]# ansible-playbook adhttp.yml --tags=config_httpd
- PLAY [cache] *****************************************************************
- TASK [Gathering Facts] *******************************************************
- ok: [cache]
- TASK [copy] ******************************************************************
- ok: [cache]
- PLAY RECAP *******************************************************************
- 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:默認,優先級最低
- ...
- tasks:
- - include: tasks/setup.yml
- - include: tasks/users.yml user=plj
- handlers:
- - include: handlers/handlers.yml
步驟五:debug檢測
- [root@ansible ansible]# ansible-playbook --syntax-check http.yml
- playbook: http.yml
- [root@ansible ansible]# ansible-playbook -C http.yml
- [root@ansible ansible]# ansible-playbook http.yml --list-tasks
- playbook: http.yml
- play #1 (cache): cache TAGS: []
- tasks:
- install one specific version of Apache TAGS: []
- lineinfile TAGS: []
- replace TAGS: []
- service TAGS: []
- copy TAGS: []
- [root@ansible ansible]# vim debug.yml
- ---
- - hosts: cache
- remote_user: root
- tasks:
- - shell: uptime |awk '{printf("%f\n",$(NF-2))}'
- register: result
- - shell: touch /tmp/isreboot
- when: result.stdout|float > 0.5
- - name: Show debug info
- debug: var=result
- [root@ansible ansible]# ansible-playbook debug.yml
- PLAY [cache] ************************************************************************************************************
- TASK [Gathering Facts] **************************************************************************************************
- ok: [cache]
- TASK [command] **********************************************************************************************************
- changed: [cache]
- TASK [command] **********************************************************************************************************
- skipping: [cache]
- TASK [Show debug info] **************************************************************************************************
- ok: [cache] => {
- "result": {
- "changed": true,
- "cmd": "uptime |awk '{printf(\"%f\\n\",$(NF-2))}'",
- "delta": "0:00:00.005905",
- "end": "2018-09-07 12:57:51.371013",
- "failed": false,
- "rc": 0,
- "start": "2018-09-07 12:57:51.365108",
- "stderr": "",
- "stderr_lines": [],
- "stdout": "0.000000",
- "stdout_lines": [
- "0.000000"
- ]
- }
- }
- PLAY RECAP **************************************************************************************************************
- cache : ok=3 changed=1 unreachable=0 failed=0