# [loop][3]
## 标准循环 with_items
~~~
# 模式1
- name: add several users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2
~~~
~~~
# 模式2
- name: add several users
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name: 'testuser1', groups: 'wheel' }
- { name: 'testuser2', groups: 'root' }
~~~
## 嵌套循环
~~~
---
- name: test
hosts: masters
tasks:
- name: give users access to multiple databases
command: "echo name={{ item[0] }} priv={{ item[1] }} test={{ item[2] }}"
with_nested:
- [ 'alice', 'bob' ]
- [ 'clientdb', 'employeedb', 'providerdb' ]
- [ '1', '2', ]
result:
changed: [localhost] => (item=[u'alice', u'clientdb', u'1'])
changed: [localhost] => (item=[u'alice', u'clientdb', u'2'])
changed: [localhost] => (item=[u'alice', u'employeedb', u'1'])
changed: [localhost] => (item=[u'alice', u'employeedb', u'2'])
changed: [localhost] => (item=[u'alice', u'providerdb', u'1'])
changed: [localhost] => (item=[u'alice', u'providerdb', u'2'])
changed: [localhost] => (item=[u'bob', u'clientdb', u'1'])
changed: [localhost] => (item=[u'bob', u'clientdb', u'2'])
changed: [localhost] => (item=[u'bob', u'employeedb', u'1'])
changed: [localhost] => (item=[u'bob', u'employeedb', u'2'])
changed: [localhost] => (item=[u'bob', u'providerdb', u'1'])
changed: [localhost] => (item=[u'bob', u'providerdb', u'2'])
~~~
## 文件循环(with_file, with_fileglob)
with_file 是将每个文件的文件内容作为item的值
with_fileglob 是将每个文件的全路径作为item的值, 在文件目录下是非递归的, 如果是在role里面应用改循环, 默认路径是roles/role_name/files_directory
~~~
- copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
with_fileglob:
- /playbooks/files/fooapp/*
~~~
## 字典循环 with_dict
~~~
---
users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
bob:
name: Bob Bananarama
telephone: 987-654-3210
可以访问的变量
tasks:
- name: Print phone records
debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
with_dict: "{{ users }}"
~~~
## with_together
~~~
tasks:
- command: echo "msg={{ item.0 }} and {{ item.1 }}"
with_together:
- [ 1, 2, 3 ]
- [ 4, 5 ]
result:
changed: [localhost] => (item=[1, 4])
changed: [localhost] => (item=[2, 5])
changed: [localhost] => (item=[3, None])
~~~
## [with_sequence][1]
~~~
---
- hosts: all
tasks:
# create groups
- group: name=evens state=present
- group: name=odds state=present
# create some test users
- user: name={{ item }} state=present groups=evens
with_sequence: start=0 end=32 format=testuser%02x
# create a series of directories with even numbers for some reason
- file: dest=/var/stuff/{{ item }} state=directory
with_sequence: start=4 end=16 stride=2
# a simpler way to use the sequence plugin
# create 4 groups
- group: name=group{{ item }} state=present
with_sequence: count=4
~~~
## 循环一个执行结果 with_lines
~~~
---
- name: test
hosts: all
tasks:
- name: Example of looping over a command result
shell: touch /$HOME/{{ item }}
with_lines: /usr/bin/cat /home/fg/test
with_lines 中的命令永远都是在controller的host上运行, 只有shell命令才会在inventory中指定的机器上运行
~~~
## 随机选择 with_random_choice
with_random_choice:在提供的list中随机选择一个值
## [with_inventory_hostnames][2]
~~~
# show all the hosts in the inventory
- debug:
msg: "{{ item }}"
with_items:
- "{{ groups['all'] }}"
# show all the hosts in the current play
- debug:
msg: "{{ item }}"
with_items:
- "{{ ansible_play_batch }}"
# show all the hosts in the inventory
- debug:
msg: "{{ item }}"
with_inventory_hostnames:
- all
# show all the hosts matching the pattern, ie all but the group www
- debug:
msg: "{{ item }}"
with_inventory_hostnames:
- all:!www
~~~
[1]:http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-integer-sequences
[2]:http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-the-inventory
[3]:http://docs.ansible.com/ansible/latest/playbooks_loops.html#
- 目录
- ansible基础
- ansible简介
- ansible安装和测试
- ansible配置文件
- 常用命令
- yaml在ansible中的用法
- inventory
- 变量与facts
- when语句
- handler模块
- 大杂烩
- ansible模块
- assert 模块
- copy模块
- cron模块
- debug模块
- django_manage模块
- file模块
- filesystem模块
- git模块
- hostname模块
- lineinfile模块
- mount模块
- mysql_user模块
- mysql_db模块
- pip模块
- selinux
- setup模块
- shell 和 command 模块
- stat模块
- supervisorctl
- systemd
- timezone
- unarchive模块
- user模块
- wait_for
- yum和service模块
- 其他模块或者方法
- setup模块
- url模块
- slack 模块
- pause 模块
- 其他
- 报错处理
- playbooks
- 复杂的playbook
- 循环
- roles
- YAML
- jinja2