为什么我不能提示一个将由多个播放共享的变量(ansible 1.6.5) [英] Why I cannot prompt for a variable that will be shared by multiple plays (ansible 1.6.5)

查看:17
本文介绍了为什么我不能提示一个将由多个播放共享的变量(ansible 1.6.5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提炼了一个包含三个剧本的剧本.目标是在一次播放中从提示中收集数据库密码,然后在其他两次播放中使用相同的密码.

I have distilled a playbook that has three plays. The goal is to collect the database password from a prompt in one play and then use the same password in the other two plays.

---

- name: database password
  hosts:
    - webservers
    - dbservers
  vars_prompt:
    - name: "db_password"
      prompt: "Enter Database Password for databse user root"
      default: "root"

- hosts: dbservers
  tasks:
    - command: echo {{db_password | mandatory }}


- hosts: webservers
  tasks:
    - command: echo {{db_password | mandatory }}

失败,如下所示.

Enter Database Password for databse user root [root]: 

PLAY [database password] ****************************************************** 

GATHERING FACTS *************************************************************** 
ok: [vc-dev-1]

PLAY [dbservers] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [vc-dev-1]

TASK: [command echo {{db_password | mandatory}}] *************************** 
fatal: [vc-dev-1] => One or more undefined variables: 'db_password' is undefined

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @.../playbook2.retry

vc-dev-1                   : ok=3    changed=0    unreachable=1    failed=0   

推荐答案

我发现了以下解决方法,使用 set_fact 将用户输入的变量分配到具有剧本范围的变量中.似乎 var_prompt 变量不像事实和其他变量,它的范围限制在提示它们的剧本中,而不是整个剧本.我不确定这是功能还是错误.

I have found the following workaround using set_fact to assign the variable entered by a user into a variable with playbook scope. It seems that var_prompt variables are not like facts and other variables, its scope is restricted in the play that prompts for them not the entire playbook. I am not sure if this is a feature or a bug.

---

- name: database password
  hosts:
    - webservers
    - dbservers
  vars_prompt:
    - name: "db_password"
      prompt: "Enter Database Password for databse user root"
      default: "root"
  tasks:
    - set_fact:
        db_root_password: "{{db_password}}"

- hosts: dbservers
  tasks:
    - command: echo {{db_root_password | mandatory }}


- hosts: webservers
  tasks:
    - command: echo {{db_root_password | mandatory }}

这篇关于为什么我不能提示一个将由多个播放共享的变量(ansible 1.6.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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