How to run plays on all hosts in a group using localhost

I’m trying to create a playbook that creates EC2 snapshots of some AWS Windows servers. I’m having a lot of trouble understanding how to get the correct directives in place to make sure things are running as they can. What I need to do is:

  • run the AWS commands locally, i.e. on the AWX host (as this is preferable to having to configure credentials on every server)
  • run the commands against each host in the inventory

I’ve done this in the past with a different group of Linux servers with no issue. But the fact that I’m having these issues makes me think that’s not working as I think it is (I’m pretty new to all things Ansible/AWX).

The first step is I need to identify instances that are usually turned off and turn them on, then to take snapshots, then to turn them off again if they are usually turned off. So this is my main.yml:

`

In the end I used delegation for this and managed to get it working. This is the amended code:


---
- name: make sure instances are running 
  hosts: all 
  gather_facts: false
  tasks:
    - name: start the instances 
      delegate_to: localhost 
      ec2:
        instance_id: "{{ instance_id }}"
        region: "{{ aws_region }}"
        state: running 
        wait: true
      register: ec2 
    - name: pause for 120 seconds to allow the instance to start 
      pause: seconds=120

I got stuck for a bit on authentication, before realising I had to use two sets of credentials for the template - one IAM user with permissions to start the instances, and one for machine credentials for the Windows servers.