標準循環python
模式一 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - testuser1 - testuser2
or
with_items: "{{ somelist }}"
模式2. 字典循環 - name: add several users user: name={{ item.name }} state=present groups={{ item.groups }} with_items: - { name: 'testuser1', groups: 'wheel' } - { name: 'testuser2', groups: 'root' }
嵌套循環web
--- - 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_dict)shell
假設字典以下 --- 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_file, with_fileglob)app
with_file 是將每一個文件的文件內容做爲item的值
dom
with_fileglob 是將每一個文件的全路徑做爲item的值, 在文件目錄下是非遞歸的, 若是是在role裏面應用改循環, 默認路徑是roles/role_name/files_directory
ide
例如: - copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600 with_fileglob: - /playbooks/files/fooapp/*
with_togetheroop
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_subelements)ui
with_subelements 有點相似與嵌套循環, 只不過第一個參數是個dict, 第二個參數是dict下的一個子項.this
整數序列(with_sequence)spa
with_sequence 產生一個遞增的整數序列,
--- - 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_random_choice)
with_random_choice:在提供的list中隨機選擇一個值
Do-util
- action: shell /usr/bin/foo register: result until: result.stdout.find("all systems go") != -1 retries: 5 delay: 10
第一個文件匹配(with_first_found)
- name: some configuration template template: src={{ item }} dest=/etc/file.cfg mode=0444 owner=root group=root with_first_found: - files: - "{{ inventory_hostname }}/etc/file.cfg" paths: - ../../../templates.overwrites - ../../../templates - files: - etc/file.cfg paths: - templates
循環一個執行結果(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中指定的機器上運行
帶序列號的list循環(with_indexed_items)
ini 文件循環(with_ini)
[section1] value1=section1/value1 value2=section1/value2 [section2] value1=section2/value1 value2=section2/value2 Here is an example of using with_ini: - debug: msg="{{ item }}" with_ini: value[1-2] section=section1 file=lookup.ini re=true
flatten循環(with_flattened)
--- - name: test hosts: all tasks: - name: Example of looping over a command result shell: echo {{ item }} with_flattened: - [1, 2, 3] - [[3,4 ]] - [ ['red-package'], ['blue-package']] :result changed: [localhost] => (item=1) changed: [localhost] => (item=2) changed: [localhost] => (item=3) changed: [localhost] => (item=3) changed: [localhost] => (item=4) changed: [localhost] => (item=red-package) changed: [localhost] => (item=blue-package)
register循環
- shell: echo "{{ item }}" with_items: - one - two register: echo 變量echo是一個字典, 字典中result是一個list, list中包含了每個item的執行結果
inventory循環(with_inventory_hostnames)
# 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
條件判斷
ansible的條件判斷很是簡單關鍵字是when, 有兩種方式
1. python語法支持的原生態格式 conditions> 1 or conditions == "ss", in, not 等等
2. ;ansible Jinja2 「filters」
tasks: - command: /bin/false register: result ignore_errors: True - command: /bin/something when: result|failed - command: /bin/something_else when: result|succeeded - command: /bin/still/something_else when: result|skipped tasks: - shell: echo "I've got '{{ foo }}' and am not afraid to use it!" when: foo is defined - fail: msg="Bailing out. this play requires 'bar'" when: bar is undefined
條件判斷能夠個loop role 和include一塊兒混用
#when 和 循環 tasks: - command: echo {{ item }} with_items: [ 0, 2, 4, 6, 8, 10 ] when: item > 5 #when和include - include: tasks/sometasks.yml when: "'reticulating splines' in output" #when 和角色 - hosts: webservers roles: - { role: debian_stock_config, when: ansible_os_family == 'Debian' }