ansible playbook 學習

同時創建多個目錄的yml,

- hosts: testhost
  vars_files:
    - /tmp/vars.yml
  tasks:
    - name: use disk name make dir
      command:  mkdir -p "/tmp/{{ dict_1.a }}/{{ dict_1.b }}/{{ dict_1.c }}"
         "/tmp/{{ dict_2['a'] }}/{{ dict_2['b'] }}/{{ dict_2['c'] }}"
         "/tmp/{{ list_1[0] }}/{{ list_1[1] }}/{{ list_1[2] }}"
         "/tmp/{{ list_2[0] }}/{{ list_2[1] }}/{{ list_2[2] }}"


額外變量文件 vars.yml

---
dict_1:    #yml 註釋可以隨便折騰
  a: AA    #縮進必須用空格
  b: BB
  c: CC

dict_2: { a: D1, b: D1, c: D1 }  #字典也可以這樣


list_1:
  - 11
  - 22
  - 33

list_2: [ L1, L2, L3 ]           #列表也可以這樣

結果:

root@svntest:/tmp# ll
total 40
drwxrwxrwx  6 root root 20480 May 17 14:14 ./
drwxr-xr-x 24 root root  4096 Dec 18  2013 ../
drwxr-xr-x  3 root root  4096 May 17 14:14 11/
drwxr-xr-x  3 root root  4096 May 17 14:14 AA/
drwxr-xr-x  3 root root  4096 May 17 14:14 D1/
drwxr-xr-x  3 root root  4096 May 17 14:14 L1/



判斷apache2進程是否存在,不存在就啓動

---
- hosts: testhost
  tasks:
   - shell: netstat -tunlp | grep ":80 " | wc -l
     register: result

#   - debug: msg="MSG {{ result }} END"

   - command: /etc/init.d/apache2 start
     when: result.stdout == "0"
     
- hosts: testc
  tasks:
   - debug: msg="EEE {{ hostvars.testhost.result.stdout }}" #訪問 testhost 的result變量


使用 /etc/ansible/hosts 中的相關主機

[test]
testhost
centostest

相關 yml 調用

- debug: msg="{{ groups.test[0] }}"


附:jinja2講法

   Yaml說明

   Yaml在線解析

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章