内部变量指的是把变量定义在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