ANSIBLE8
循环项的分量:---
- name: Users Exist and Are in the Correct Groups
hosts: srvgroup
tasks:
- name: User and Group
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.group }}"
loop:
- name: alex
group: wheel
- name: natasha
group: root
其他方式的循环:
---
- name: Copy File
hosts: server1.example.com
tasks:
- name: Test Copy
copy:
src: "{{ item }}"
dest: /tmp/
with_lines:
- ls ./v5.d/*
- name: Watch It
shell:
cat "{{ item }}"
loop:
- /tmp/again.html
- /tmp/again2.html
register: v_result
- name: Result
debug:
var: v_result
---
- name: Copy File
hosts: server2.example.com
tasks:
- name: Test Copy
copy:
src: "{{ item }}"
dest: /tmp/
with_fileglob:
- ./v5.d/*
- name: Watch It
shell:
cat "{{ item }}"
loop:
- /tmp/again.html
- /tmp/again2.html
register: v_result
- name: Result
debug:
var: v_result
在循环结果中挑选输出(循环项的分量):
---
- name: Loop Register Test
gather_facts: no
hosts: localhost
tasks:
- name: Loop Echo Task
shell:
echo welcome to "{{ item }}"
loop:
- RHCE
- RHCA
register: v_result
- name: Show debug Information
debug:
msg: |
"{{ item.stdout }}"
loop:
"{{ v_result.results }}"
(让debug有循环)上面改造之后的剧本:
---
- name: Copy File
hosts: server2.example.com
tasks:
- name: Test Copy
copy:
src: "{{ item }}"
dest: /tmp/
with_fileglob:
- ./v5.d/*
- name: Watch It
shell:
cat "{{ item }}"
loop:
- /tmp/again.html
- /tmp/again2.html
register: v_result
- name: Result
debug:
msg: "{{ item.stdout }}"
loop: "{{ v_result.results }}"
页:
[1]