Panos D
Panos D

Reputation: 45

Problem with simple conditional on variable

I can't make this simple conditional to work.

I use pause in a role for user confirmation and I register the input to variable.
I use this variable in the next 2 tasks:

  1. one to run when input is y
  2. the other when is n

But I can't make the condition to work based on the user input.

Below is the tasks/main.yml


# tasks file for Update-AdvFirewall-to-V1.6
 - name: Get Note
   routeros_command:
     commands: /system note print
   register: output
      
 - name: Get AdvFirewall Version
   set_fact:
     installed: "{{ item | replace('- AdvFirewall=V', '') | trim }}"
   when: "'AdvFirewall=V' in item"
   loop: "{{ output.stdout_lines[0] }}"

 - name: Confirm if not Installed
   pause:
     prompt: "AdvFirewall is not installed. Do you want to install (y/n)"
   when: installed is undefined
   register: confirm_new_install

 - name: Install AdvFirewall if not installed
   routeros_command:
     commands:
       - system note set note="Installed ADVfirewall"
   when: confirm_new_install == "y"
  
 - meta: end_play
   when: confirm_new_install is defined or installed is undefined

 - name: Upgrade AdvFirewall if needed
   routeros_command:
     commands:
       - system note set note="Time to upgrade"
   when: installed == "1.5"

 - name: Send Email if AdvFirewall is older than required
   routeros_command:
     commands:
       - "/tool e-mail send to=\"[email protected]\" subject=\"AdvFirewall Upgrade | Installed Version doesn`t meet requirements for upgrade on $[/system identity get name]\" body=\"$[/system identity get name] has AdvFirewall V{{ installed }}. Required Version is V{{ required }}\""
   when: installed < "1.5"

Edit: After all it works with some changes i figured out with your help.

- name: Install AdvFirewall if not installed
    routeros_command:
      commands:
        - system note set note="Installed ADVfirewall"
      when: "confirm_new_install.user_input | default('X') == 'y'"
      
- meta: end_play
   when: "confirm_new_install.user_input | default('X') != 'X'"

Thank you very much!

Upvotes: 1

Views: 94

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39079

The pause module, as most of Ansible modules register a dictionary, where the user input is under the user_input key.

But also, because your pause task can skip, you'll have to test if it is defined, or add a default.

So you have to change your condition to:

  • either
    when: "confirm_new_install.user_input | default('n') == 'y'"
    
  • or
    when: 
      - confirm_new_install.user_input is defined
      - confirm_new_install.user_input == "y"
    

Remember that you can always debug any output of Ansible in order to identify those kind of issues.

Using:

- debug:
    var: confirm_new_install

Would have showed you something like:

{
    "confirm_new_install": {
        "changed": false,
        "delta": 2,
        "echo": true,
        "failed": false,
        "rc": 0,
        "start": "2021-02-01 19:55:09.835787",
        "stderr": "",
        "stdout": "Paused for 0.04 minutes",
        "stop": "2021-02-01 19:55:12.448872",
        "user_input": "y"
    }
}

Upvotes: 1

Related Questions