我知道如何使用Ansible創建AWS實例。現在我想要實現的是通過使用創建實例的相同Playbook安裝nginx將該實例配置爲Web服務器。獲取使用Ansible創建的AWS實例的IP地址/屬性
的劇本的目標將是:
- 創建一個AWS實例。
- 通過設置Nginx服務器將實例配置爲Web服務器。
它是否可能與ansible?
我知道如何使用Ansible創建AWS實例。現在我想要實現的是通過使用創建實例的相同Playbook安裝nginx將該實例配置爲Web服務器。獲取使用Ansible創建的AWS實例的IP地址/屬性
的劇本的目標將是:
它是否可能與ansible?
閱讀http://www.ansible.com/blog/ansible-ec2-tags它詳細說明了如何創建一個ec2實例(或多個),然後針對它運行任務(即安裝nginx)。
I'f要直接跳到例如劇本https://github.com/chrismeyersfsu/playbook-ec2_properties/blob/master/new_group.yml
注:你將與你的任務集合安裝nginx的
@Bidyut 更換ping任務如何
看看Line 27注意使用register: ec2
然後在Line 46的EC2參考EC2的IP地址ip地址是「提取」{{ ec2.results[item.0]['instances'][0]['public_ip'] }}
。請注意,該示例在循環中調用register
。如果您只是創建一個ec2實例,那麼ec2 ip地址引用看起來像{{ ec2.results['instances'][0]['public_ip'] }}
這是一個可以幫助你的工作示例。
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Create the EC2 Instance
ec2:
region: us-east-1
group: sg-xxxxx # Replace your Security Group here
keypair: test-key # Replace Key here
instance_type: t2.mirco
image: ami-xxxxx # Replace AMI here
vpc_subnet_id: subnet-xxxxx # Replace Subnet here
assign_public_ip: yes
wait: yes
wait_timeout: 600
instance_tags:
Name: "My-EC2-Instance"
register: ec2
- name: Create SSH Group to login dynamically to EC2 Instance
add_host:
hostname: "{{ item.public_ip }}"
groupname: ec2_server
with_items: ec2.instances
- name: Wait for SSH to come up
wait_for:
host: "{{ item.public_ip }}"
port: 22
state: started
with_items: ec2.instances
- hosts: ec2_server
become: yes
# Use ec2_user if you are using CentOS/Amazon server
remote_user: ubuntu # for Ubuntu server
gather_facts: yes
roles:
- webserver
您能否解釋如何從Ansible操作手冊中的AWS返回的JSON結果獲取/獲取特定字段。我試圖谷歌它,但我沒有找到任何。 – Bidyut
該行的'item'是什麼? – Bidyut
在'public_ip'中它的值是None – Bidyut