ansible - Ansible 条件查找
问题描述
我有一本剧本,其中的任务如下所示。两者push_config
和import_consul
都没有通过,所以默认情况下有 false。
- name: checkout config-tibco repository
git:
repo: ssh://git@bitbucket.corp.contingo.com:7999/gwa/config-tibco.git
dest: "/opt/awx/tmp/config-tibco/"
when: "{{ push_config | default(false) | bool }} or {{ import_consul | default(false) | bool }}"
...
....
.....
- name: Update Consul KV Store
shell: curl --request PUT -d "{{item.split('=',1) [1]}}" "http://{{ consul_host }}:{{ consul_port }}/v1/kv//TEST/{{ bw_application_name.stdout }}/{{ app_profile }}/{{item.split('=',1) [0]}}"
loop: "{{ lookup('file', '/opt/awx/tmp/config-tibco/properties/*.properties').splitlines() }}"
when: "{{ import_consul | default(false) | bool }}"
我期待第二个任务 -更新 Consul KV Store应该被简单地忽略,因为条件应该是错误的。第二个任务中预期的查找文件将仅在条件为真时下载/检出(即import_consul
),因此它在第一个任务中从 GIT 检出并可供第二个任务使用。
但是我在查找文件方面遇到了异常。这里的问题是,当条件已经设置为 false 时,为什么还要尝试评估任务模块。
fatal: [host machine]: FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: /opt/awx/tmp/config-tibco/properties/*.properties"}
有什么建议么?
解决方案
问:原始消息:在查找中找不到文件:/opt/awx/tmp/config-tibco/properties/*.properties
答:when
必须在每次迭代中评估条件,因为条件可能取决于item
. 因此,loop
必须在 之前评估when
。
笔记
为避免错误,请检查文件是否存在,如果文件不存在则终止播放。
始终至少创建一个空文件
default.properties
条件(第
when
一个和第二个)将导致警告
[警告]:条件语句不应包含 jinja2 模板分隔符,例如 {{ }} 或 {% %}。找到:{{ import_consul|default(false)|bool }}
修复它(第一个同上)
when: import_consul|default(false)|bool
推荐阅读
- java - RatingBar 错误 - 尝试在空对象引用上调用虚拟方法
- ios - 创建缺少的 ios 配置导致“应用程序尚未注册错误”
- java - 使用 Mysql 在 Java 中使用外键从表中设置和获取列
- python - 张量流中的字符串连接
- angular - 如何从枚举类型中分配值
- javascript - Onmousedown javascript未触发
- algorithm - 我应该使用什么算法来解决“最短路径问题”?
- c# - 异常构造函数是否应该抛出异常?
- php - 如果模型中每个元素的条件,如何为 API 创建 xml 而不是手动创建 xml?
- c++ - 结构内联初始化的向量。支撑初始化列表