1

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!

1 Answer 1

1

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"
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It works but a new problem appears. I get error because when it finds AdvFirewall inside note it skips pause for input with error result.
Mhm? Do you mean it skips pause then the next tasks errors?
if installed var is defined then it skips user confirmation and continues to the next task (The plan is if AdvFirewall is V1.5 upgrade to 1.6, if version is lower send email that you must run an older update first and if its not installed ask for confirmation to fresh install it). But skipping user input means that confirm_new_install.user_input stays empty. That is what causes the error. Is there any way of fixing this? I have tried ignore_errors: yes but its not working
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.