为什么我不能提示要由多个剧本共享的变量(可使用1.6.5) [英] Why I cannot prompt for a variable that will be shared by multiple plays (ansible 1.6.5)

查看:75
本文介绍了为什么我不能提示要由多个剧本共享的变量(可使用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将用户输入的变量分配给具有Playbook范围的变量的变通方法.似乎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 }}

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

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