0

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:

  1. I want to run tasks like restarting nginx and add tag on instance launched by amazon auto-scaling group.

  2. 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.

Vaibhav Mule
  • 103
  • 5

2 Answers2

3

Above your 'Add EP2 instances to host group' task, use

- debug: var=ec2

to see what the variable holds. If you look at the variables for each instance you'll probably find you've spelt ec2_publicIP wrong or it's in a different location in the variable.

xddsg
  • 3,392
  • 2
  • 28
  • 33
0

As I just want to run command on instance launched by 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
Vaibhav Mule
  • 103
  • 5