ansible - 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 列表中。
我应该怎么做才能获得变量的正确值?
解决方案
如果我理解正确:
---
- 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 ***************************************************************************
如果我没有很好理解,请澄清
推荐阅读
- file - TeamCity:是否可以从 file:// 共享使用 Subversion SVN?
- html - 如何将按钮从切换更改为单状态?
- c++ - 来自调试代码的未使用变量警告
- kotlin - 如何从文本文件中读取和转换 Kotlin DSL 定义?
- python - 使用带有 /ugcPosts 端点的创建图像共享 API 创建的帖子在我的时间轴上的linkedin 上不可见
- android - Viewpager 内容未使用 ScrollView 在片段中显示,但显示滚动条
- c# - Veracode 在 Actionresult 中传递模型的输入验证不足问题
- ios - 如何在uitableviewcell中更新约束动画
- java - 启动层初始化期间发生错误:java.lang.module.ResolutionException
- pandas - 拆分熊猫对象