Search code examples
ansibleprompt

Ansible - How to include answer from previous prompt into next prompt


I am working on a playbook, where I get the values for variables from console. I am trying to use the answer from first question in the prompt for the second question. But that does not work. Is it possible to do so?

This is what I have tried.

vars_prompt:

    - name: acl_username
      prompt: "User name for ACL"
      private: no

    - name: acl_password
      prompt: "Password for "
      #      prompt: "Password for {{ acl_username }}"
      private: yes

I do realise that in my initial testing, the password will be displayed, but once I get it working, I will search for how not to output the task details.

Thank you


Solution

  • You'll need to prompt for user input in tasks rather than in the vars_prompt section; variables in vars_prompt aren't available until after all the data has been collected.

    You can use the poorly named pause module to prompt for user input in a task. For example:

    - hosts: localhost
      gather_facts: false
      tasks:
        - name: get username
          pause:
            prompt: "User name for ACL"
            echo: true
          register: acl_username
    
        - name: get password
          pause:
            prompt: "Password for {{ acl_username.user_input }}"
            echo: false
          register: acl_password
    
        - debug:
            msg:
              - "{{ acl_username.user_input }}"
              - "{{ acl_password.user_input }}"
    

    Running this will look something like:

    PLAY [localhost] *****************************************************************************************************************************************************************************
    
    TASK [get username] **************************************************************************************************************************************************************************
    [get username]
    User name for ACL:
    ok: [localhost]
    
    TASK [get password] **************************************************************************************************************************************************************************
    [get password]
    Password for alice (output is hidden):
    ok: [localhost]
    
    TASK [debug] *********************************************************************************************************************************************************************************
    ok: [localhost] => {
        "msg": [
            "alice",
            "secret"
        ]
    }
    
    PLAY RECAP ***********************************************************************************************************************************************************************************
    localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0