Prashant Dhimal
Prashant Dhimal

Reputation: 41

Display prompt based on when condition

I want to prompt a certain question after a certain value is given to another prompt in Ansible, but I'm not being able to do that.

I want domain decision to appear after answering option 1, it should not ask for domain decision if the answer is different than 1.

Code:

- name: os_system
  private: no
  prompt: |
  Which os do you want to use?
   1- Windows Server
   2- CentOS_7
   3- CentOs_8
   4- Ubuntu
   5- Others
- name: Domain Decision
  private: no
  prompt: Do you want your PC in Domain
  when: (os_system == "1" )

Upvotes: 1

Views: 62

Answers (1)

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

Reputation: 39089

You cannot use when statement in vars_prompt, as answered in their issue tracker.

This said, you can use pause that also lets you prompt for variables and use them via the user_input return value.

Given the playbook:

- hosts: all
  gather_facts: no    
        
  tasks:
    - name: OS System
      pause:
        prompt: |
          Which OS do you want to use?
          1- Windows Server
          2- CentOS 7
          3- CentOS 8
          4- Ubuntu
          5- Others
      register: os_system
    - name: Domain Decision
      pause:
        prompt: In which domain do you want your PC?
      when: os_system.user_input == "1"

Here are two run of it:

  • When answering 2
    PLAY [all] *********************************************************************
    
    TASK [OS System] ***************************************************************
    [os_system]
    Which OS do you want to use?
    1- Windows Server
    2- CentOS 7
    3- CentOS 8
    4- Ubuntu
    5- Others
    :
    ok: [localhost] 
    
    TASK [Domain Decision] *********************************************************
    skipping: [localhost]
    
    PLAY RECAP *********************************************************************
    localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
    
  • When answering 1
    PLAY [all] *********************************************************************
    
    TASK [OS System] ***************************************************************
    [os_system]
    Which OS do you want to use?
    1- Windows Server
    2- CentOS 7
    3- CentOS 8
    4- Ubuntu
    5- Others
    :
    ok: [localhost]
    
    TASK [Domain Decision] *********************************************************
    [Domain Decision]
    In which domain do you want your PC?:
    ok: [localhost]
    
    PLAY RECAP *********************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
    

Upvotes: 1

Related Questions