运行全局variables

我想创build一个服务器集群,使用可靠的。

在一个主要手册中,我包含了一些子手册。

- include: playbook_commandserver.yml - include: playbook_agent.yml 

在playbook_commandserver.yml中,我创build了命令服务器(在aws上)。 然后,我触发一个angular色,它使用set_fact模块来记住commandserver的dns名称:

 - name: Get hostname of command server shell: /usr/bin/host $(/usr/bin/curl -s http://ipecho.net/plain) | /usr/bin/awk '{print $5}' | /usr/bin/awk -F 'aws.com' '{print $1"aws.com"}' register: cs - name: Set hostname of command server as fact set_fact: commandserver="{{ cs.stdout }}" 

commandserver事实可以在同一个游戏中使用,但是不能在同一个游戏手册中。更不要说在playbook_agent.yml ,后来被包含在内。 而这正是在那里,我需要访问该服务器事实。

那么如何设置/存储variables,这些variables对于完整的无效运行是有效的?

我发现这个: https : //stackoverflow.com/questions/26732241/ansible-save-registered-variable-to-file但是对我来说,这看起来像一个丑陋的黑客。

它没有更好的解决这个问题? 有没有办法设置一个variables,这是有效的整个运行?

是的,这是可能的。 当你用set_fact模块设置一个事实时,这个事实可以通过“hostvars”来访问。 所以如果你这样定义你的variablescommandserver:

  - name: Set hostname of command server as fact set_fact: commandserver="{{ cs.stdout }}" 

那么你可以用同样的方法在其他包含的剧本中访问这个variables(debugging模块只是一个例子):

  - debug: msg="{{ hostvars['put the hostname in here']['commandserver'] }}"