Starting Stopped AWS EC2 Instances from Ansible Tower

22 views
Skip to first unread message

John Peters

unread,
Sep 27, 2018, 11:29:17 AM9/27/18
to Ansible Project
Any clue as to how to accomplish this?

My Ansible Tower inventory has a list of hosts in it, all in 'stopped' state.

I'd like to use that inventory, assign it to a playbook, that will loop through each machine in the inventory, from the localhost, and issue a command to start the instance to AWS.

I've tried several different ways to do this, but to no avail.

It seems that because I'm specifying an inventory to the job, and that inventory only has stopped instances in it, there's no way to execute a task on the localhost, loop through that inventory, and start them, one by one?

Here is some code that contains all the different lines I've tried...

---
- name: Start EC2 Instances
  hosts: localhost # also tried 'all' here
  connection: local
  gather_facts: false
  tasks:
    - name: inventory_hostname var
      debug:
        msg: "{{ hostvars[inventory_hostname] }}"
      with_inventory_hostnames: all
      delegate_to: localhost

    - name: hostvars item
      debug:
        msg: "{{ hostvars[item] }}"
      with_inventory_hostnames: all
      delegate_to: localhost

   - name: Start EC2 Instances Task
     ec2_instance: running
      local_action:
        module: ec2
          instance_ids={{hostvars[inventory_hostname]['ec2_id']}}
          region="{{ region | default('us-east-1') }}"
          state=running
     when: hostvars[item]['ec2_id'] is defined
     with_inventory_hostnames: all
     delegate_to: localhost


Kuldip Madnani

unread,
Sep 27, 2018, 11:52:29 AM9/27/18
to Ansible Project
If gather_facts is false and node is unreachable, how are you going to retrieve hostvars[item]['ec2_id'] ? Is it statically declared in your inventory file? 
Reply all
Reply to author
Forward
0 new messages