Skip to content

Instantly share code, notes, and snippets.

@hortonew
Created September 17, 2018 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hortonew/42cbe87abfa7846986c2115a87899532 to your computer and use it in GitHub Desktop.
Save hortonew/42cbe87abfa7846986c2115a87899532 to your computer and use it in GitHub Desktop.
Will allow you to set variables needed before dynamic inventory is run, allowing you to use ansible vault
---
- name: Set env variables
hosts: localhost
connection: local
environment:
AWS_ACCESS_KEY_ID: "{{ AWS_ACCESS_KEY }}"
AWS_SECRET_ACCESS_KEY: "{{ AWS_SECRET_KEY }}"
tasks:
- name: Build AWS Inventory
command: "{{ ec2_py_location }} --list --refresh-cache"
register: aws_servers
- name: Create fact composed of all the new servers
set_fact:
new_aws_servers: "{{ aws_servers.stdout }}"
- name: Add hosts to in-memory inventory
add_host:
name: "{{ item }}"
groups:
- aws
with_items: "{{ new_aws_servers._meta.hostvars }}"
- name: Execute
hosts: aws
gather_facts: false
tasks:
- name: "Display the AWS servers this play will operate over"
debug:
msg: "{{inventory_hostname}}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment