Re: [ansible-project] simplify always the same conditionals - evaluate contents of a string in conditionals (changed_when, failed_when, ...)

2020-06-11 Thread Michael K.
Hi Brian, 

thanks for your advice. I've added the variable as host var

ansible_fortios_default_changed_when: "{{ 'meta' in r and 
r['meta']['status'] == 'success' and r['meta']['revision_changed'] == true 
}}"
ansible_fortios_default_failed_when: "{{ 'meta' not in r or 
r['meta']['status'] == 'error' }}"

and to the play:

...
  register: r
  changed_when: ansible_fortios_default_changed_when|bool
  failed_when: ansible_fortios_default_failed_when|bool

That worked very well - thanks! Could you elaborate on your note "Also you 
are using templating in the wrong places. " a bit more?
 
Greetings,
Michael

Am Mittwoch, 10. Juni 2020 22:40:19 UTC+2 schrieb Brian Coca:
>
> instead of set_fact (which forces static evaluation BEFORE the task, 
> use `vars:` which is lazy evaluation (on us). Also you are using 
> templating in the wrong places. 
>
>
> failed_when: my_failed_when|bool 
> vars: 
>
> my_failed_when: "{{'meta' not in r or r['meta']['status'] == 
> 'error'}}" 
>
>
> -- 
> -- 
> Brian Coca 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/a3a5a9d7-7ac7-4e5d-96c4-7e4caff5f92eo%40googlegroups.com.


[ansible-project] simplify always the same conditionals - evaluate contents of a string in conditionals (changed_when, failed_when, ...)

2020-06-09 Thread Michael K.
Hi there,

while working with fortinet.fortios I've engineered that the modules need 
some help to track the changed and failed
status. So I've added the following parameters to each module called:

- name: "{{ device }}{{ vdom }} configure static route"
  fortios_router_static:
vdom: "{{ vdom }}"
state: "present"
router_static:
  device: "{{ item.device }}"
  dst: "{{ item.dst }}"
  gateway: "{{ item.gateway }}"
  seq_num: "{{ item.name }}"
  status: "enable"
  with_items: "{{ router_static }}"
  register: r
  changed_when: "'meta' in r and r['meta']['status'] == 'success' and 
r['meta']['revision_changed'] == true"
  failed_when: "'meta' not in r or r['meta']['status'] == 'error'"


As it is kinda hard to read and maintain, I wonder if there is a simpler 
way without changing the module. Something like this comes to mind:


- set_fact
my_changed_when: "'meta' in r and r['meta']['status'] == 'success' and 
r['meta']['revision_changed'] == true"
my_failed_when: "'meta' not in r or r['meta']['status'] == 'error'"

- name: "{{ device }}{{ vdom }} configure static route"
  fortios_router_static:
vdom: "{{ vdom }}"
state: "present"
router_static:
  device: "{{ item.device }}"
  dst: "{{ item.dst }}"
  gateway: "{{ item.gateway }}"
  seq_num: "{{ item.name }}"
  status: "enable"
  with_items: "{{ router_static }}"
  register: r
  changed_when: "{{ my_changed_when }}"
  failed_when: "{{ my_failed_when}}"

The problem is, that this is not getting evaluated. Further, an error is 
thrown that jinja template tags are not allowed in conditionals anymore. 
Does anyone have another
idea how to solve this?

Greetings,
Michael

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/4d2e6101-c718-480d-b6f6-e87157642058o%40googlegroups.com.