问题原文
原帖链接 · 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:
- You need information from another server to use in the current server you're configuring.
- The other server you need information from is hosted in AWS.
- 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]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
