zoukankan      html  css  js  c++  java
  • ansible——playbook循环

    1.标准loop

    通过with_items参数实现,item会去with_items这个数组中轮询取值。
    定义normal_loos.yaml:

    ---
    - hosts: all
      gather_facts: False
      tasks:
        - name: debug loops
          debug: msg="name ------> {{ item }}"
          with_items:
            - one
            - two

    执行结果:

    当然可以支持键值对的方式:
    定义normal2_loops.yaml:

    ---
    - hosts: all
      gather_facts: False
      tasks:
        - name: debug loops
          debug: msg="name ------> {{ item.key }} value ------> {{ item.value }}"
          with_items:
            - {key: "one", value: "VALUE1"}
            - {key: "two", value: "VALUE2"}

    执行结果:

    2.嵌套Loops

    主要实现多个循环体的合并。
    定义nest_loop.yaml:

    ---
    - hosts: all
      gather_facts: False
      tasks:
        - name: debug loops
          debug: msg="name->{{ item[0] }} value->{{ item[1] }}"
          with_nested:
             - ['A']
             - ['a','b','b']

    执行结果:

    解释说明:
      with_nested是一个数组.
      item[0]表示这个数组的第一个元素,也就是['A']
      item[1]表示这个数组的第二个元素,也就是['a','b','b']
      然后分别循环这两个元素。

    3.散列loops

    标准loop支持的是数组,类似于python中的列表。
    嵌套loop其实使用是[{},{}]这种列表套字典的形式,
    而散列loop可以直接支持字典形式,只是变相要遵循yaml格式。

    定义:  hash_loop.yaml

    ---
    - hosts: all
      gather_facts: Fasle
      vars:
        user:
          kebi:
            name: zhangrongkai
            addr: luotian
          maoxian: 
            name: songlixing
            addr: xiaochang
    
      tasks:
        - name: debug loops
          debug: msg="name--->{{ item.key }} value--->{{ item.value.name }} addr---{{ item.value.addr }}"
          with_dict: "{{ user }}"

    将其转化为python中的字典格式:

    user = {
        "kebi": {
            "name": "zhangrongkai",
            "addr": "luotian"
        },
        "maoxian": {
            "name": "songlixing",
            "addr": "xiaochang"
        }
    }

    item=user,key=[kebi,maoxian],然后取其属性值。

    注意这里:with_dict: "{{ user }}"
    在2.0之前,采用的是这种格式:with_dict: user
    否则就会报错:

    4.文件配置loops

    在某些时候,我们需要针对某一类型的文件进行一些操作,这个时候就可以使用fileglob这个参数。
    通过这个fileglob就能匹配到相应的文件,然后就可以进行相应的操作。
    定义:file_loop.yaml

    ---
    - hosts: all
      tasks:
        - name: debug loops
          debug: msg="filename--->{{ item }}"
          with_fileglob:
            - /tmp/*.py

    执行结果:

    在ansible内部其实是调用python模块glob,然后进行如下操作:
      glob.glob("/tmp/*.py")

    5.随机选择loops

    随机选择item数组中的一个值。
    定义:random_loop.yaml

    ---
    - hosts: all
      tasks:
         - name: debug loops
           debug: msg="name--->{{ item }}"
           with_random_choice:
             - "test1"
             - "test2"
             - "test3"

    执行结果:

    内部其实就是使用python的random模块来实现。

    6.条件判断

    有时候,我们需要执行一个操作,这个操作可能失败,我们希望5秒之后再去尝试执行,
    这样的操作重复5次,如果还不行就退出。
    定义:case_loop.yaml

    ---
    - hosts: all
      tasks:
        - name: debug loops
          shell: cat /etc/hostname
          register: host
          until: host.stdout.startswith("centos")
          retries: 5
          delay: 5

    执行结果:

    解释说明:
      name: debug loops #任务名称,非必需
      shell: cat /etc/hostname #操作
      register: host #操作的结果赋值给host
      until: host.stdout.startswith("centos") #直到...成立
      retries: 5 #重复5次
      delay: 5 #每个5秒重复一次

    如果操作失败,是下面结果:

    7.文件优先匹配Loops

    文件优先匹配会根据你传入的变量或者文件进行从上往下匹配,如果匹配到某个文件,它会用这个文件当作{{item}}的值。

    ---
    - hosts: all
      tasks:
        - name: debug loops
          debug: msg="file--->{{ item }}"
          with_first_found:
            - "{{ ansible_distribution }}.yaml"
            - "default.yaml

    8.register Loops

    之前通过register定义ret来接收shell操作的结果。
    当时之前接收的是一个数据,但是现在确实接收多个。

    定义:  register_loop.yaml

    ---
    - hosts: all
      tasks: 
        - name: debug loops
          shell: "{{ item }}"
          with_items:
            - hostname
            - uname
          register: ret
        - name: display loops
          debug: msg="{% for i in ret.results %} {{ i.stdout }} {% endfor %}"

    执行结果:

  • 相关阅读:
    [SDOI2013]直径(树的直径)
    [ZJOI2012]旅游(树的直径)
    [SDOI2011]消防(树的直径)
    【模板】2-SAT 问题(2-SAT)
    [HNOI2006]公路修建问题
    速度限制(分层图)
    [JLOI2011]飞行路线(分层图)
    【洛谷 P3194】 [HNOI2008]水平可见直线 (单调栈)
    【洛谷 P3187】 [HNOI2007]最小矩形覆盖 (二维凸包,旋转卡壳)
    【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)
  • 原文地址:https://www.cnblogs.com/yangmingxianshen/p/12657125.html
Copyright © 2011-2022 走看看