
我已經建立了本機配置和 Amazon Auto-scaling 群組。透過使用此代碼。
---
-
local_action:
image_id: ami-61f9324
instance_type: t2.micro
module: ec2_lc
name: nice-lc
region: us-west-2
security_groups: launch-wizard-1
name: "Create lc"
-
local_action:
desired_capacity: 2
health_check_period: 5
health_check_type: ELB
launch_config_name: nice-lc
load_balancers: nice-lb1
max_size: 10
min_size: 2
module: ec2_asg
name: nice-asg
region: us-west-2
name: "Create asg"
我正在使用ec2.py
和ec2.ini
歸檔庫存。
現在,本地配置和自動縮放群組已創建,並根據我們定義的所需容量,啟動了 2 個實例。
現在我想在這兩個實例上運行任務。
為此,我使用這個 yaml 檔案。
---
- name: Example of provisioning servers
hosts: 127.0.0.1
connection: local
tasks:
- name: Add EP2 instances to host group
local_action: add_host hostname={{ ec2_publicIp }} groupname=launch-wizard-1
with_items: ec2.instances
- name: Add tag to instances
local_action: ec2_tag resource={{ item.id }} region=us-west-2 state=present
with_items: ec2.instances
args:
tags:
Name: nice-ec2
- name: Wait for SSH to be available
pause: minutes=1
- name: Configure provisioned servers
hosts: tag_aws_autoscaling_groupName_nice_asg
user: ubuntu
sudo: True
gather_facts: True
tasks:
- name: restart nginx
service: name=nginx state=restarted
當我運行該文件時,出現錯誤:
一個或多個未定義的變數:「ec2_publicIp」未定義
問題:
我想運行諸如重新啟動 nginx 之類的任務,並在亞馬遜自動縮放群組啟動的實例上添加標籤。
我做得對嗎?如果我只想在 asg 啟動的實例上執行某些任務,看起來我甚至不需要新增到主機。
答案1
在「將 EP2 實例新增至主機群組」任務上方,使用
- debug: var=ec2
查看變數保存的內容。如果您查看每個實例的變量,您可能會發現 ec2_publicIP 拼字錯誤,或者它位於變數中的不同位置。
答案2
因為我只想在 asg 啟動的實例上執行命令。
---
# I have removed that code and just using this.
# This works with instance launched by asg.
- name: Configure provisioned servers
hosts: tag_aws_autoscaling_groupName_nice_asg
user: ubuntu
sudo: True
gather_facts: True
tasks:
- name: restart nginx
service: name=nginx state=restarted