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.

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.

| improve this answer | |
  • When I run ec2.py, I get "tag_Name_neon_ec2": [ "52.25.25.210"] How can I use this in my add to host – Vaibhav Mule May 21 '15 at 14:36
  • use that variable in your with_items – xddsg May 21 '15 at 14:40
  • it doesn't help me. – Vaibhav Mule May 22 '15 at 14:52
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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.