EC2 dynamic enventory. Fixes #73

pull/262/head
Jack Ivanov 7 years ago
parent 5cbf125202
commit f7da2e3888

@ -58,3 +58,10 @@ SSH_keys:
comment: algo@ssh
private: configs/algo.pem
public: configs/algo.pem.pub
dynamic_inventory_groups:
- azure
- digitalocean
- ec2
- gce
- local

@ -12,3 +12,14 @@
- name: Change mode for the SSH private key
local_action: file path=configs/algo.pem mode=0600
- name: Ensure the dynamic inventory exists
blockinfile:
dest: configs/inventory.dynamic
marker: "# {mark} ALGO MANAGED BLOCK"
create: yes
block: |
[algo:children]
{% for group in dynamic_inventory_groups %}
{{ group }}
{% endfor %}

@ -96,6 +96,7 @@
region: "{{ region }}"
instance_tags:
name: "{{ aws_server_name }}"
Environment: Algo
exact_count: 1
count_tag:
name: "{{ aws_server_name }}"
@ -115,3 +116,29 @@
- set_fact:
cloud_instance_ip: "{{ ec2.tagged_instances[0].public_ip }}"
- name: Get EC2 instances
ec2_remote_facts:
aws_access_key: "{{ aws_access_key | default(lookup('env','AWS_ACCESS_KEY_ID'))}}"
aws_secret_key: "{{ aws_secret_key | default(lookup('env','AWS_SECRET_ACCESS_KEY'))}}"
region: "{{ region }}"
filters:
instance-state-name: running
"tag:Environment": Algo
register: algo_instances
- name: Ensure the group ec2 exists in the dynamic inventory file
lineinfile:
state: present
dest: configs/inventory.dynamic
line: '[ec2]'
- name: Populate the dynamic inventory
lineinfile:
state: present
dest: configs/inventory.dynamic
insertafter: '\[ec2\]'
regexp: "^{{ item.public_ip_address }}.*"
line: "{{ item.public_ip_address }}"
with_items:
- "{{ algo_instances.instances }}"

Loading…
Cancel
Save