Ansible to Conditionally Prompt for a Variable?

前端 未结 6 1414
遇见更好的自我
遇见更好的自我 2020-12-28 13:04

I would like to be able to prompt for my super secure password variable if it is not already in the environment variables. (I\'m thinking that I might not want to put the de

6条回答
  •  误落风尘
    2020-12-28 13:37

    I might be late to the party but a quick way to avoid vars_prompt is to disable the interactive mode by doing that simple trick:

    echo -n | ansible-playbook -e MyVar=blih site.yaml
    

    This add no control over which vars_prompt to avoid but coupled with default: "my_default" it can be used in a script.

    Full example here:

    ---
    - hosts: localhost
      vars_prompt:
        - prompt: Enter blah value
        - default: "{{ my_blah }}"
        - name: blah
    

    echo -n | ansible-playbook -e my_blah=blih site.yaml

    EDIT:

    I've found that using the pause module and the prompt argument was doing what I wanted:

    ---
    - pause:
          prompt: "Sudo password for localhost "
      when: ( env == 'local' ) and
          ( inventory_hostname == "localhost" ) and
          ( hostvars["localhost"]["ansible_become_password"] is not defined )
      register: sudo_password
      no_log: true
      tags:
           - always
    

提交回复
热议问题