Ansible-当条件在Playbook级别对vars_prompt不起作用时 [英] Ansible - when conditional not working for vars_prompt at playbook level

查看:188
本文介绍了Ansible-当条件在Playbook级别对vars_prompt不起作用时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可用2.1.2.0

我在这里遇到一种情况,我正在尝试与大家一起检查是否可以使用Ansible vars_prompt 功能和 when 有条件的解决方案./p>

Ansible具有此功能,称为: vars_prompt ,我想将其与何时有条件( when:... 我们通常在任务或剧本级操作中使用的任务)

PS:我的问题与这篇文章不同:

工作区/ansible/install_tool.yml中,我有:

 -主机:全部成为:真实collect_facts:否vars:实例:测试#install_tool:否company_api_url:DUMMY_INVALID_URLvars_prompt:-名称:"company_api_url"提示:输入公司API URL:"默认值:"https://{{instance}}.company.com"私人:否什么时候:install_tool =="true"和company_api_url =="DUMMY_INVALID_URL"# 或者#when:(("{{install_tool}}" =="true"和"{{company_api_url}}" =="DUMMY_INVALID_URL")前任务:-调试:msg ="install_tool = {{install_tool}}和instance = {{instance}}"什么时候:install_tool =="true" 

我的问题:
如何获得上述条件提示(使用提示变量的有效的替换 default 值)以及 SKIP 来提示用户取决于 install_tool 变量的值?

我的理解在顶级剧本文件( install_tool.yml )中,我正在定义一个值为 test 的变量 instance .为什么在剧本级别定义vars_prompt部分的 default 值时不选择 test 并替换其值(如果用户通过了在命令行中 ex: -extra-vars"instance = qa" 的实例变量?在这种情况下,我希望默认值为 [https://qa.company.com] ,我该怎么做?运行任何语法时都看不到任何错误.

我得到以下输出(但是我期望一个替换的URL值,例如: [https://test.company.com] ) 即使在命令行中将 install_tool 设置或传递为 false :

时也会提示

运行:

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

  $ ansible-playbook -i库存-l localhost install_tool.yml --extra-vars"install_tool = false"........一些行在这里...手册:install_tool.yml ***********************************************在install_tool.yml中扮演1个角色输入公司API网址:[https://{{instance}}.company.com]: 

vars_prompt 部分中(在下/针对)下的条件(<"{{install_tool}}" =="true"和...)-名称:company_api_url 变量,它没有因任何语法问题或其他错误而出错(这意味着Ansible并不认为这是错误的),然后为什么,Ansible仍在提示我即使在我传递-extra-vars"install_tool = false" -OR-的情况下,也可以输入上面的 vars_prompts 变量我取消注释这一行: vars:部分下的 install_tool:false 吗?

PS: 何时:... 条件成功地用于 pre_tasks 部分,即,当install_tool为true时,它显示的值为这两个变量,并且将install_tool设置为false时,它不会运行-debug 步骤并跳过它(这是预期的).

谢谢.

解决方案

您可以检查提示的vars的所有受支持属性不支持.

防止vars提示的唯一方法是通过extra_vars传递有问题的变量.在您的示例中,如果您通过 -e company_api_url = my_custom_url ,则Ansible将跳过提示.

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.

解决方案

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.

这篇关于Ansible-当条件在Playbook级别对vars_prompt不起作用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆