zgdy 发表于 2018-7-29 07:45:36

ansible之条件和循环

条件
when
  可用于task,role和include,在满足条件时task才会被执行。至于when指令后跟的逻辑表达式也是标准的逻辑表达式,示例如下:
tasks:  
- shell: echo "only on Red Hat 6, derivatives, and later"
  
    when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int >= 6
  
- shell: echo "This certainly is epic!"
  
    when: epic is defined
循环
标准遍历
  用with_items可以遍历一个列表,注意这里只会遍历一层。示例如下:
- name: add several users  
user: name={{ item }} state=present groups=wheel
  
with_items:
  
    - testuser1
  
    - testuser2
嵌套遍历
  用with_nested可以遍历一个列表,注意这里会遍历多层,直到最内层。示例如下:
-name: give users access to multiple databases  
    mysql_user: name={{ item }} priv={{ item }}.*:ALL append_privs=yes password=foo
  
    with_nested:
  
    - [ 'alice', 'bob', 'eve' ]
  
    - [ 'clientdb', 'employeedb', 'providerdb' ]
遍历字典
  用with_dict可以遍历一个字典,用key和value来表示。示例如下:
  变量文件
---  
users:
  
alice:
  
    name: Alice Appleworth
  
    telephone: 123-456-7890
  
bob:
  
    name: Bob Bananarama
  
    telephone: 987-654-3210
  playbook文件
tasks:  
   -name: Print phone records
  
      debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
  
      with_dict: users
文件通配符循环
  用with_fileglob可以获取本地文件列表。示例如下:
    # copy each file over that matches the given pattern  
    - copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
  
      with_fileglob:
  
      - /playbooks/files/fooapp/*
对齐的列表
  用with_together可以达到类似python里的zip函数的功能。示例如下:
  变量文件:
---  
alpha: [ 'a', 'b', 'c', 'd' ]
  
numbers:[ 1, 2, 3, 4 ]
  playbook文件
tasks:  
- debug: msg="{{ item.0 }} and {{ item.1 }}"
  
    with_together:
  
    - alpha
  
    - numbers
子元素循环
  with_subelements这个比较费解。
数字序列循环
  可以通过with_sequence来生成一个数字序列,其参数包括:

[*]  start起始数字
[*]  end结束数字
[*]  stride步长
[*]  count个数
[*]  format输出的字符串
  示例如下:
---- hosts: alltasks:    # 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从一个序列里随机取一个元素。示例如下:
- debug: msg={{ item }}  
    with_random_choice:
  
      - "go through the door"
  
      - "drink from the goblet"
  
      - "press the red button"
  
      - "do nothing"
until循环
  这种循环由三个指令完成:
  * until是一个条件表达式,如果满足条件循环结束
  * retry是重试的次数
  * delay是延迟时间
  示例如下:
- action: shell /usr/bin/foo  
register: result
  
until: result.stdout.find("all systems go") != -1
  
retries: 5
  
delay: 10
循环直到找到文件
  与with_items类似,只是with_first_found找到列表里的第一个文件就会终止循环。示例如下:
- name: INTERFACES | Create Ansible header for /etc/network/interfaces  
    template: src={{ item }} dest=/etc/foo.conf
  
    with_first_found:
  
      - "{{ansible_virtualization_type}}_foo.conf"
  
      - "default_foo.conf"
循环一个task的输出
  with_lines指令后跟一个命令,ansible会遍历命令的输出。示例如下:
- name: Example of looping over a command result  
    shell: /usr/bin/frobnicate {{ item }}
  
    with_lines: /usr/bin/frobnications_per_host --param {{ inventory_hostname }}
带索引地循环列表
  与with_items类似,with_indexed_items会把列表索引和对应元素放到一个列表里。示例如下:
- name: indexed loop demo  
    debug: msg="at array position {{ item.0 }} there is a value {{ item.1 }}"
  
    with_indexed_items: some_list
扁平化循环列表
  with_flattened会先拍扁一个列表,然后执行with_items。示例如下:
- name: flattened loop demo  
   yum: name={{ item }} state=installed
  
   with_flattened:
  
   - ['nc','git', ['nmap', 'vim']]
配合register循环列表
  register注册一个变量后,可以配合with_items来遍历变量结果。示例如下:
- shell: echo "{{ item }}"  
    with_items:
  
    - one
  
    - two
  
    register: echo
  
- name: Fail if return code is not 0
  
    fail:
  
    msg: "The command ({{ item.cmd }}) did not have a 0 return code"
  
    when: item.rc != 0
  
    with_items: echo.results
页: [1]
查看完整版本: ansible之条件和循环