循环遍历with_subelements ansible

时间:2018-06-04 22:23:49

标签: ansible ansible-2.x ansible-inventory ansible-facts

您有以下变量:

couchbase:
 - name: incre1
   ipaddress:
    - 10.16.9.177
    - 10.16.9.178
   buckets:
    - AA1
    - aa1

我的电子书有以下内容:

 - debug:
    msg: "Running backup as {{CBBACKUPMGR}} backup -r {{ item.1 }} --cluster couchbase://{{ item.0.ipaddress }}"
   register: example
   with_subelements:
     - "{{ couchbase }}"
     -  buckets

我想迭代ipaddress然后使用存储桶,所以基本上我想看看:

Running backup as /opt/ouchbase backup -r AA1 --cluster couchbase://10.16.9.177
Running backup as /opt/ouchbase backup -r aa1 --cluster couchbase://10.16.9.177
Running backup as /opt/ouchbase backup -r AA1 --cluster couchbase://10.16.9.178
Running backup as /opt/ouchbase backup -r aa1 --cluster couchbase://10.16.9.178

但是,在运行剧本时,我看到如下:

Running backup as /opt/ouchbase backup -r AA1 --cluster couchbase://[u'10.16.9.177', u'10.16.9.178']
Running backup as /opt/ouchbase backup -r aa1 --cluster couchbase://[u'10.16.9.177', u'10.16.9.178']

1 个答案:

答案 0 :(得分:0)

那不是with_subelements所做的。如果您使用此“调试”循环来打印{{item}},您将看到在每次迭代时,它会创建一个列表:

  1. 包含您指定的子元素的couchbase列表中的父元素,没有该子元素的哈希值和
  2. 该迭代的子元素的值。
  3. 这是输出:

    TASK [debug] ********************************************************************************************************************************************************************************************************
    ok: [localhost] => (item=None) => {
        "msg": [
            {
                "ipaddress": [
                    "10.16.9.177", 
                    "10.16.9.178"
                ], 
                "name": "incre1"
            }, 
            "AA1"
        ]
    }
    ok: [localhost] => (item=None) => {
        "msg": [
            {
                "ipaddress": [
                    "10.16.9.177", 
                    "10.16.9.178"
                ], 
                "name": "incre1"
            }, 
            "aa1"
        ]
    }
    
    PLAY RECAP
    

    在您澄清时,您的目的是在ipaddressbuckets之间生成所有可能的组合。

    要实现这一目标,请尝试以下任务:

      - debug:
          msg: "Running backup as {{CBBACKUPMGR}} backup -r {{ item[0] }} --cluster couchbase://{{ item[1] }}"
        register: example
        with_items:
          - "{{ lookup('nested', couchbase[0].ipaddress, couchbase[0].buckets) }}"
    

    这假设您将couchbase列表变量只包含一个元素,就像您的示例中一样。

    结果:

    TASK [debug] ********************************************************************************************************************************************************************************************************
    ok: [localhost] => (item=None) => {
        "msg": "Running backup as /opt/ouchbase backup -r 10.16.9.177 --cluster couchbase://AA1"
    }
    ok: [localhost] => (item=None) => {
        "msg": "Running backup as /opt/ouchbase backup -r 10.16.9.177 --cluster couchbase://aa1"
    }
    ok: [localhost] => (item=None) => {
        "msg": "Running backup as /opt/ouchbase backup -r 10.16.9.178 --cluster couchbase://AA1"
    }
    ok: [localhost] => (item=None) => {
        "msg": "Running backup as /opt/ouchbase backup -r 10.16.9.178 --cluster couchbase://aa1"
    }
    
    PLAY RECAP
    
    希望它有所帮助。