2
votes

ansible 2.1.2.0

I have a situation here for which I'm trying to check with you all to see if the solution is even possible with Ansible vars_prompt feature and when conditional.

Ansible has this feature called: vars_prompt and I want to club this with the when conditional (when: ... which we generally use in tasks at task or playbook level actions)

PS: My question is different than this post: Ansible to Conditionally Prompt for a Variable? i.e. my vars: variables are not dependent upon any machine's OS ENV variables, in my case, I just have a simple hardcoded value for vars: variables. I'm also running the when condition at the main playbook level (and not at the task's level).

What I'm trying to do is to prompt a user for entering a variable value, only if at command line, the user has called the ansible-playbook install_tool.yml ... ... with --extra-vars "install_tool=true"

In workspace/ansible/install_tool.yml I have:

- hosts: all
  become: true
  gather_facts: false

  vars:
    instance: test
    #install_tool: false
    company_api_url: DUMMY_INVALID_URL

  vars_prompt:
    - name: "company_api_url"
      prompt: "Enter Company API URL: "
      default: "https://{{ instance }}.company.com"
      private: no
      when: install_tool == "true" and company_api_url == "DUMMY_INVALID_URL"
      # or
      #when: ( "{{ install_tool }}" == "true" and "{{ company_api_url }}" == "DUMMY_INVALID_URL" )

  pre_tasks:
    - debug: msg="install_tool = {{ install_tool }} and instance = {{ instance }}" 
      when: install_tool == "true"

My question:
How can I get the above conditional prompt working (with a valid substituted default value for a prompt variable) and also SKIP to prompt a user altogether depending upon the value of install_tool variable?

My understanding: In top level playbook file (install_tool.yml), I'm defining a variable instance with value as test. Why the default value for the vars_prompt section did NOT pick test and substituted its value when it's defined at the playbook level (--OR what if a user is passing instance variable at command line for ex: --extra-vars "instance=qa"? In that case, I would want the default value as [https://qa.company.com], how can I do that)? I don't see any errors while running it for any syntax.

I'm getting the following output (but I was expecting a substituted URL value like: [https://test.company.com]) and it's still prompting even when install_tool was set or passed at command line as false:

Running:

$ ansible-playbook -i "localhost," --connection=local install_tool.yml --extra-vars "install_tool=false instance=qa"

or

$ ansible-playbook -i inventory -l localhost install_tool.yml --extra-vars "install_tool=false"
...
.....some lines here
...
PLAYBOOK: install_tool.yml *******************************************
1 plays in install_tool.yml
Enter Company API URL:  [https://{{ instance }}.company.com]: 

As using when: ( "{{ install_tool }}" == "true" and ...) condition within vars_prompt section, under/for - name: company_api_url variable, it didn't error out for any syntax issues or etc (that means, Ansible doesn't think it's wrong), then WHY, Ansible is still prompting me to enter the above vars_prompts variable, even when I'm passing --extra-vars "install_tool=false" --OR-- if I uncomment the line: install_tool: false under vars: section?

PS: when: ... condition is successfully working for the pre_tasks section i.e. when install_tool is true, it shows the value of those 2 variables and when install_tool is set to false, then it does NOT run the - debug step and skips it (which is expected).

Thanks.

1
Nail it down to one single question and present a reproducible example.ceving
@ceving I changed my post, does that help? I thought to provide some explanation in detail. Does the above install_tool.yml work at your end for showing a substituted default value and is the prompt part skipped when install_tool is set to false?AKS
It doesn't look like vars_prompt: uses the when: directive at all... if you just add a simple when: False you can see that it is ignored.Rob H
@RobH You are right. I wonder if under pre_tasks, I can use some native module, prompt a user conditionally (in some cases typos etc to make sure things dont break at 98% of the run) and register the variable but I guess that's not going to work as command line variable will superceed all or is that possible. It would have been good to have this feature to make Ansible both interactive/non-interactive at least for Ansible INTERACTIVE runs. For non-interactive runs (if -e var=value is not correct), then it's OK for Ansible to fail if the passed value was incorrect for some reason (typo)AKS

1 Answers

3
votes

You can check all supported attributes for prompted vars here.

when statement is not supported for prompted vars. And never was.

Templated default values for prompted vars is not supported also.

The only way to prevent vars prompt is to pass the variable in question via extra_vars. In your example, if you pass -e company_api_url=my_custom_url, Ansible will skip prompting for it.