首页 > 解决方案 > Ansible 在联合操作期间更改了事实值

问题描述

我想向 Ansible 列表添加一个新对象,所以我创建了一个如下所示的代码:

- debug:
   msg: "{{distinguished_name_with_env }}"

- debug:
   msg: "{{_current_topics_operations }}"

# Add operations to global Ansible variable
- name: Add principal operations to global ACL topic list
  set_fact:
    _current_topics_operations: "{{ _current_topics_operations | union([ { 'distinguished_name': distinguished_name_with_env , 'operations':  item, 'topic_name':  topic_name  }]) }}"
  loop: "{{_topic_operations}}"
  run_once: True

并且有一个输出:

TASK [confluent.acl : debug] ********************************************************************************************************************************************************************************
Monday 16 August 2021  16:02:56 +0200 (0:00:00.061)       0:00:18.834 ********* 
ok: [localhost] => 
  msg: user_1-dev
ASK [confluent.acl : debug] ********************************************************************************************************************************************************************************
Monday 16 August 2021  16:02:56 +0200 (0:00:00.067)       0:00:18.902 ********* 
ok: [localhost] => 
  msg:[]
TASK [confluent.acl : to add  gldownload_topic_1] ***********************************************************************************************************************************************************
Monday 16 August 2021  16:02:57 +0200 (0:00:00.057)       0:00:19.412 ********* 
ok: [localhost] => 
  msg:
  - distinguished_name: write-dev
    operations: write
    topic_name: download_topic_1
  - distinguished_name: describe-dev
    operations: describe
    topic_name: download_topic_1

问题在于添加到列表中的新对象。Distinguished_name 值与执行联合操作之前不同。

在执行联合操作之前:

distinguished_name_with_env  -> user_1-dev

在联合操作期间:

distinguished_name_with_env -> describe-dev
distinguished_name_with_env -> write-dev

'Describe' 和 'write' 存储在 _topic_operations 列表中。

我应该怎么做才能获得变量的正确值?

标签: ansible

解决方案


如果我理解正确:

---
- hosts: localhost
  gather_facts: false

  vars:
    distinguished_name_with_env: user_1-dev
    topic_name: download_topic_1
    _topic_operations:
      - describe-dev
      - write-dev

  tasks:

    # Add operations to global Ansible variable
    - name: Add principal operations to global ACL topic list
      set_fact:
        _current_topics_operations: "{{ _current_topics_operations
                                        | default([])
                                        | union( [ {'distinguished_name': distinguished_name_with_env ,
                                                    'operations': item,
                                                    'topic_name': topic_name } ]) }}"
      loop: "{{ _topic_operations }}"
      run_once: True

    - debug:
        msg: "{{ _current_topics_operations }}"

和输出:

PLAY [localhost] *********************************************************************

TASK [Add principal operations to global ACL topic list] *****************************
ok: [localhost] => (item=describe-dev)
ok: [localhost] => (item=write-dev)

TASK [debug] *************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "distinguished_name": "user_1-dev",
            "operations": "describe-dev",
            "topic_name": "download_topic_1"
        },
        {
            "distinguished_name": "user_1-dev",
            "operations": "write-dev",
            "topic_name": "download_topic_1"
        }
    ]
}

PLAY RECAP ***************************************************************************

如果我没有很好理解,请澄清


推荐阅读