Ansible - when conditional not working for vars_prompt at playbook level

人走茶凉 提交于 2021-01-27 19:33:38

问题


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:


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.



来源:https://stackoverflow.com/questions/41424122/ansible-when-conditional-not-working-for-vars-prompt-at-playbook-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!