I have created local configuration and Amazon Auto-scaling group. By using this code.
---
-
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"
I'm using ec2.py
and ec2.ini
files for inventory.
So now Local configuration and Auto-scaling group created and according desired capacity we have defined, 2 instance are launched.
Now I want to run tasks on those 2 instance.
For that I'm using this yaml file.
---
- 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
When I run this file, I get error:
One or more undefined variables: 'ec2_publicIp' is undefined
Questions:
I want to run tasks like restarting nginx and add tag on instance launched by amazon auto-scaling group.
Am I doing right? Looks I do not even need add to host if I just want to run some task on instance launched by asg.