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

Ansible, AWS, other host based on tag

· 52CE 我爱测

I am creating Ansible playbooks which create our system's infrastructure in AWS. Obviously dynamic inventories using ec2.py are involved. I first create a utility server, then a web server, and I have a separate playbook for each…

问题原文

原帖链接 · sheetzam · CC BY-SA 3.0

I am creating Ansible playbooks which create our system's infrastructure in AWS. Obviously dynamic inventories using ec2.py are involved.

I first create a utility server, then a web server, and I have a separate playbook for each of these. I have files on the web server which need to have the private ip of the utility server added using a template.

What's the best way to retrieve the private ip of the utility server for use in the web server's playbook?

已采纳解答

原帖链接 · sheetzam · CC BY-SA 3.0

There were three problems:

  1. You need information from another server to use in the current server you're configuring.
  2. The other server you need information from is hosted in AWS.
  3. The other server you need to access information about has a variable name.

The solution to 3 is to specify the variable in the command line using extra-vars like so: ansible-playbook -i ./ec2.py --extra-vars "chaos=test" aws_playground.yaml

The solution to 2 is to find the hostname you need from the hostvars' groups variable. When combined with 3 above, you want something like: - debug: var=hostvars[groups['tag_Name_'+chaos+'_util'][0]].ansible_all_ipv4_addresses[0]

The solution to 1 is to first collect facts from the other server before working on the server you're configuring. Below is a playbook collecting all this in one place:


# Called like: ansible-playbook -i ./ec2.py --extra-vars "chaos=test" aws_playground.yaml
- name: Collect our facts on util
  hosts: "tag_Name_{{ chaos }}_util"
  tasks: [ ]

- name: Set up a web server
  hosts: "tag_Name_{{ chaos }}_web"
  tasks:
   - debug: var=hostvars[groups['tag_Name_'+chaos+'_util'][0]].ansible_all_ipv4_addresses[0]

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

资料来源与适用范围

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

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

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