大网数据广告活动
服务器运维问答

ansible run global variable

· 52CE 我爱测

I want to create a cluster of servers, using ansible. In one main-playbook, I include some sub-playbooks. - include: playbook_commandserver.yml - include: playbook_agent.yml In the playbook_commandserver.yml, I create the command…

问题原文

原帖链接 · doh-nutz · CC BY-SA 3.0

I want to create a cluster of servers, using ansible.

In one main-playbook, I include some sub-playbooks.

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

In the playbook_commandserver.yml, I create the command-server (on aws). Then, I trigger a role, that uses the set_fact module to remember the commandserver's dns name:

- 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 }}"

The commandserver fact is available in the same play, but not in the same playbook .. let alone in the playbook_agent.yml, that gets included, afterwards. And it's exactly there, where I would need to access that commandserver-fact.

So how does one set/store variables, that are valid for the complete ansible-run?

I've found this: https://stackoverflow.com/questions/26732241/ansible-save-registered-variable-to-file However to me this looks like an ugly hack.

Is ther no better solution to this problem? Is there no way to set a variable, that is valid for the whole ansible-run?

已采纳解答

原帖链接 · A.Hab · CC BY-SA 3.0

Yes, this is possible. When you set a fact with the set_fact module, this fact is accessible via "hostvars". So if you define your variable commandserver like so:

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

then you can access this variable in other included playbooks of the same play in that way (the debug module is just an example):

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-08-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。