设为首页 收藏本站
查看: 924|回复: 0

[经验分享] ansible之条件和循环

[复制链接]

尚未签到

发表于 2018-7-29 07:45:36 | 显示全部楼层 |阅读模式
条件
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[0] }} priv={{ item[1] }}.*: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: 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从一个序列里随机取一个元素。示例如下:
- 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-542768-1-1.html 上篇帖子: Ansible之roles介绍 下篇帖子: Ansible 输出结果保存至 excel
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表