內部變量指的是把變量定義在playbook裏面或者是執行結果做爲變量shell
[root@LeoDevops playb]# cat p_loop.yaml - hosts: u12 gather_facts: False tasks: - name: debug loops debug: msg="name -------> {{ item }}" with_items: - one - two
運行結果以下:bash
[root@LeoDevops playb]# ansible-playbook p_loop.yaml PLAY [u12] ************************************************************************************************************************************************************************************ TASK [debug loops] **************************************************************************************************************************************************************************** ok: [192.168.93.137] => (item=one) => { "changed": false, "item": "one", "msg": "name -------> one" } ok: [192.168.93.137] => (item=two) => { "changed": false, "item": "two", "msg": "name -------> two" } PLAY RECAP ************************************************************************************************************************************************************************************ 192.168.93.137 : ok=1 changed=0 unreachable=0 failed=0
固然也支持字典格式的數據對於要循環的內容數據結構
[root@LeoDevops playb]# cat p_loop.yaml - hosts: u12 gather_facts: False tasks: - name: debug loops debug: msg="key--> {{ item.key }} value ---> {{ item.value }}" with_items: - {"key":"name","value":"one"} - {"key":"name","value":"two"} - {"key":"name","value":"three"} - {"key":"name","value":"four"}
[root@LeoDevops playb]# cat p_iframe_loop.yaml - hosts: u12 gather_facts: False tasks: - name: debug loops debug: msg="name --> {{ item[0] }} value ---> {{ item[1] }}" with_nested: - [1] - ['a','b','c']
執行效果以下:dom
[root@LeoDevops playb]# ansible-playbook p_iframe_loop.yaml PLAY [u12] ************************************************************************************************************************************************************************************ TASK [debug loops] **************************************************************************************************************************************************************************** ok: [192.168.93.137] => (item=[1, u'a']) => { "changed": false, "item": [ 1, "a" ], "msg": "name --> 1 value ---> a" } ok: [192.168.93.137] => (item=[1, u'b']) => { "changed": false, "item": [ 1, "b" ], "msg": "name --> 1 value ---> b" } ok: [192.168.93.137] => (item=[1, u'c']) => { "changed": false, "item": [ 1, "c" ], "msg": "name --> 1 value ---> c" } PLAY RECAP ************************************************************************************************************************************************************************************ 192.168.93.137 : ok=1 changed=0 unreachable=0 failed=0
散列loops比標準的loops就是變量支持更豐富的數據結構,好比標準的loops的最外層數據必須是Python 的List數據類型,而散列loops直接支持YAML格式的數據變量。例以下面的簡單例子:oop
[root@LeoDevops playb]# cat p-sanlieloop.yaml - hosts: u12 gather_facts: False vars: users: yq: name: yq shell: bash ljf: name: ljf shell: bash tasks: - name: test for sanlie loop debug: msg="name --> {{ item.key }} value --> {{ item.value.name }} shell ---> {{ item.value.shell }}" with_dict: users
針對一個目錄下制定格式的文件進行處理,這個時候直接使用with_fileglob循環去匹配咱們須要處理的文件便可。debug
[root@LeoDevops playb]# cat p-fileloop.yaml - hosts: u12 gather_facts: False tasks: - name: debug file loop debug: msg="files ---> {{ item }}" with_fileglob: - /tmp/playb/*.yaml
隨機數code
[root@LeoDevops playb]# cat p-randomloop.yaml - hosts: u12 gather_facts: False tasks: - name: debug loops debug: msg="name -->> {{ item }}" with_random_choice: - "a1" - "a2" - "a3" - "a4"
有時候task執行一個task後,咱們須要檢測這個task的結果是否達到了預想狀態,若是沒有達到咱們預想的狀態,那麼就須要退出整個playbook執行,那麼這個時候咱們就須要對某個task結果一直循環檢測了。three
[root@LeoDevops playb]# cat p-conditionloop.yaml - hosts: u12 tasks: - name: debug loop shell: hostname register: pwd until: pwd.stdout.startswith("LeoTestMachine") # stdout與stdout_lines是不同的,這個要注意哦 retries: 3 # 重複3次 delay: 2 # 間隔2秒
[root@LeoDevops playb]# cat p-filefilter.yaml - hosts: u12 gather_facts: True tasks: - name: debug codes debug: msg="files --->{{ item }}" with_first_found: - "{{ ansible_distribution }}.yaml" - "default.yaml"
register不但用於task直接互相傳遞數據,也能夠吧register用在單一的task中進行變量臨時存儲,而且能夠接受多個task結果當作變量臨時存儲。iframe
[root@LeoDevops playb]# cat register_loops.yaml - hosts: u12 gather_facts: True tasks: - name: debug loops shell: "{{ item }}" with_items: - hostname - uname register: ret - name: display loops debug: msg="{% for i in ret.results %}--->{{ i.stdout }}{% endfor %}"
執行多個task而且register給一個變量時,它的數據就和日常的不同了,就須要使用jinja2語法顯示出來it