Ansible条件查询

时间:2019-12-30 21:11:28

标签: ansible

我有一本剧本,其中的任务如下所示。 push_configimport_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 应该被忽略,因为条件应该为false。 second 任务中期望的查找文件仅在条件成立时(即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"}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

  

Q:原始消息:在查找中找不到文件:/ opt / awx / tmp / config-tibco / properties / *。properties

A:必须在每次迭代中评估with recursive customer_relation AS ( select id, parent_id ancestor_id, 1 distance from favor_customer where parent_id is not null union all select c.id, r.ancestor_id, distance+1 from favor_customer c inner join customer_relation r on r.id = c.parent_id ) select c.id, c.nickname, (select count(*) from customer_relation where ancestor_id = c.id and distance = 1) level_1, (select count(*) from customer_relation where ancestor_id = c.id and distance = 2) level_2 from favor_customer c 条件,因为该条件可能取决于#include <gps.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <math.h> #include <errno.h> int main() { int rc; //struct timeval tv; struct gps_data_t gps_data; if ((gps_open(GPSD_SHARED_MEMORY, NULL, &gps_data)) == -1) { printf("code: %d, reason: %s\n", errno, gps_errstr(errno)); return EXIT_FAILURE; } for(int i=0; i<10; i++) { /* read data */ if ((gps_read(&gps_data,NULL,0)) == -1) { printf("error occured reading gps data. code: %d, reason: %s\n", errno, gps_errstr(errno)); } else { /* Display data from the GPS receiver. */ if ((gps_data.status == STATUS_FIX) && (gps_data.fix.mode == MODE_2D || gps_data.fix.mode == MODE_3D) && !isnan(gps_data.fix.latitude) && !isnan(gps_data.fix.longitude)) { printf("latitude: %f, longitude: %f, speed: %f, timestamp: %lf\n", gps_data.fix.latitude, gps_data.fix.longitude, gps_data.fix.speed, gps_data.fix.time); } else { printf("no GPS data available\n"); } } sleep(3); } /* When you are done... */ gps_close (&gps_data); return EXIT_SUCCESS; } 。因此,必须先评估when来评估item


笔记

  • file插件不适用于通配符。请改用fileglob

  • 为避免错误,请检查文件是否存在,如果文件不存在,请终止播放。

  • 始终至少创建一个空文件loop

  • when条件(第一和第二)将导致警告

  

[警告]:条件语句不应包含jinja2模板分隔符,例如{{}}或{%%}。找到:{{   import_consul | default(false)| bool}}

修复(第一个同上)

default.properties