Highlighted
  • 233 Views

ansible fail module?

Hi! I am having a problem with fail module in ansible. When I used it in a playbook, its executing fine but when I am trying to do the same thing with roles, its not working. I have defined exactly all variables in vars/main.yml and tasks/main.yml. All the tasks before the fail module are executing fine. Irrespective of the values defined, if I give max < total|int, it is always skipping and if I give max > total|int, it is always failing.

vars:

    max: 5000

    objects:

        - value: 1035

        - value: 3076

    total: 0

tasks:

    - set_fact:

          total: "{{ total }} + objects[{{ item }}].value"

       with_sequence: start=0 end="{{ objects | length - 1 }}"

    - debug:

          var:  "{{  total }}"

    - fail:

         msg:  "Failed"

      when: total|int  > max

Labels (1)
0 Kudos
1 Reply
Highlighted
Starfighter Starfighter
Starfighter
  • 223 Views

Re: ansible fail module?

Hi!
The problem you have is not with fail module.
The problem you have when you set_fact and get total variable as string, but not an integer. This string give 0 being converted to integer.
Please, look at simple correction for set_fact:
- name: test int
hosts: localhost
vars:
max: 5000
objects:
- value: 1035
- value: 3076
total: 0
tasks:
- set_fact:
total: "{{ total|int + item.value }}"
loop: "{{ objects }}"
- debug:
msg: "{{ total }}"
- fail:
msg: "Failed"
when: total|int > max

0 Kudos
Reply
Loading...
Join the discussion
You must log in to join this conversation.