Skip to content

Commit

Permalink
Replace ec2_ami_find with ec2_ami_facts
Browse files Browse the repository at this point in the history
  • Loading branch information
Szpadel committed Apr 3, 2020
1 parent 5c9ad97 commit 10789d8
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 46 deletions.
7 changes: 4 additions & 3 deletions requirements-python.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# highest version provisioning runs fully on
ansible>=2.7,<2.8
# DO NOT upgrade to >=2.9.3 until
# https://github.com/ansible/ansible/issues/66641
ansible==2.9.2

# needed for inventory and aws modules
boto3
Expand All @@ -21,4 +22,4 @@ docker-py
awscli

# required for awscli
PyYAML<5.3
PyYAML<5.3
14 changes: 6 additions & 8 deletions roles/cs.aws-create-ami/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
register: ami_output

- name: Wait until AMI is accessible
ec2_ami_find:
ec2_ami_info:
region: "{{ aws_region }}"
owner: self
sort: creationDate
sort_order: descending
no_result_action: fail
ami_tags: "{{ aws_tags_base | combine(ami_tags) }}"
register: ami_found
until: ami_found.results is defined and ami_found.results[0].ami_id == ami_output.image_id and ami_found.results[0].state == "available"
filters: "{{ aws_tags_base | prefix_keys('tag:') | combine(ami_facts_tag_filters) }}"
vars:
ami_facts_tag_filters: "{{ ami_tags | prefix_keys('tag:') }}"
register: ami_facts
until: "ami_facts.images is defined and ami_facts.images|length > 0"
retries: 60
delay: 5
when: ami_output is changed

2 changes: 2 additions & 0 deletions roles/cs.aws-ec2-cleanup/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- role: cs.ansible-plugins
20 changes: 8 additions & 12 deletions roles/cs.aws-ec2-cleanup/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@
msg: "Keeping {{ _aws_ec2_cleanup_ami_ids_to_keep | count }} AMIs: {{ _aws_ec2_cleanup_ami_ids_to_keep | join(', ') }}"

- name: Get list of App AMIs
ec2_ami_find:
ec2_ami_info:
region: "{{ aws_region }}"
owner: self
sort: creationDate
sort_order: descending
ami_tags:
"Role": "app"
"Project": "{{ mageops_project }}"
"Environment": "{{ mageops_environment }}"
filters: "{{ aws_tags_base | prefix_keys('tag:') | combine(aws_tags_role_app_filter) }}"
vars:
aws_tags_role_app_filter: "{{ aws_tags_role_app | prefix_keys('tag:') }}"
register: _aws_ec2_cleanup_env_amis

- name: "Remove Launch Configurations not longer required"
Expand All @@ -56,9 +53,8 @@
region: "{{ aws_region }}"
state: absent
delete_snapshot: yes
image_id: "{{ item.ami_id }}"
with_items: "{{ _aws_ec2_cleanup_env_amis.results }}"
image_id: "{{ item.image_id }}"
with_items: "{{ _aws_ec2_cleanup_env_amis.images }}"
when:
- not item | skipped
- item.ami_id not in _aws_ec2_cleanup_ami_ids_to_keep

- item is not skipped
- item.image_id not in _aws_ec2_cleanup_ami_ids_to_keep
3 changes: 1 addition & 2 deletions roles/cs.aws-node-facts/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: Get EC2 instance information for nodes
ec2_instance_facts:
ec2_instance_info:
region: "{{ aws_region }}"
filters: "{{ aws_node_facts_base_filters | combine(aws_node_facts_base_tags | combine(node.tags) | prefix_keys('tag:')) }}"
loop:
Expand Down Expand Up @@ -72,4 +72,3 @@
set_fact:
mageops_redis_sessions_host: "{{ aws_nodes_info.redis.private_ip_address }}"
when: not mageops_redis_sessions_host | default(false, true) and aws_nodes_info.redis

8 changes: 4 additions & 4 deletions roles/cs.aws-node-state/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
- name: Find EC2 instances to be processed
ec2_instance_facts:
ec2_instance_info:
region: "{{ aws_region }}"
filters: "{{ node_state_instance_filters | combine(node_state_instance_tags | prefix_keys('tag:')) }}"
register: node_state_ec2_instance_facts
register: node_state_ec2_instance_info

- name: Enforce target state instances found
ec2:
region: "{{ aws_region }}"
state: "{{ node_state_target }}"
instance_ids: "{{ node_state_ec2_instance_facts.instances | map(attribute='instance_id') | list }}"
instance_ids: "{{ node_state_ec2_instance_info.instances | map(attribute='instance_id') | list }}"
wait: yes
when: node_state_ec2_instance_facts.instances | length > 0
when: node_state_ec2_instance_info.instances | length > 0
10 changes: 5 additions & 5 deletions roles/cs.varnish/tasks/000-facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
- name: Get VPC IP addresses of backend app instances
delegate_to: localhost
become: no
ec2_instance_facts:
ec2_instance_info:
region: "{{ aws_region }}"
filters: "{{ varnish_backend_instances_filter_base | combine(varnish_backend_instances_aws_tags | prefix_keys('tag:')) }}"
register: _varnish_backends_ec2_instance_facts
register: _varnish_backends_ec2_instance_info

- name: Set Varnish backend instances if any are running (AWS)
set_fact:
varnish_backend_instances_app: "{{ _varnish_backends_ec2_instance_facts.instances }}"
when: _varnish_backends_ec2_instance_facts.instances | length > 0
varnish_backend_instances_app: "{{ _varnish_backends_ec2_instance_info.instances }}"
when: _varnish_backends_ec2_instance_info.instances | length > 0
when: varnish_standalone and aws_use

- name: Check if throttling shall be enabled
set_fact:
varnish_throttling_enabled: "{{ (varnish_standalone and varnish_throttling and varnish_throttling_rules | length > 0)|bool }}"
varnish_throttling_enabled: "{{ (varnish_standalone and varnish_throttling and varnish_throttling_rules | length > 0)|bool }}"
2 changes: 1 addition & 1 deletion site.maintenance.aws-remove-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
- name: Remove other EC2 Servers
block:
- name: Get list of EC2 instances
ec2_instance_facts:
ec2_instance_info:
region: "{{ aws_region }}"
filters:
"vpc-id": "{{ _aws_vpc_id }}"
Expand Down
10 changes: 5 additions & 5 deletions site.maintenance.aws-start.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
when: start_ec2
block:
- name: Get list of EC2 instances
ec2_instance_facts:
ec2_instance_info:
region: "{{ aws_region }}"
filters:
"vpc-id": "{{ aws_vpc_id }}"
register: start_ec2_instance_facts
register: start_ec2_instance_info

- name: Start instances
ec2:
Expand All @@ -24,7 +24,7 @@
wait_timeout: 600
wait: yes
vars:
start_ec2_instance_ids: "{{ start_ec2_instance_facts | json_query('instances[].instance_id') }}"
start_ec2_instance_ids: "{{ start_ec2_instance_info | json_query('instances[].instance_id') }}"

- name: Scale up App Node ASGs
when: start_asg
Expand All @@ -48,7 +48,7 @@
with_items: "{{ start_asgs }}"
vars:
start_asgs: "{{ start_asg_facts | json_query('results[].{ name: auto_scaling_group_name, lc: launch_configuration_name}') }}"

- name: Scale up Extra App Node ASGs
when: start_asg and (aws_extra_app_asg_enable or magento_aws_ondemand_import_instance_enable)
block:
Expand All @@ -70,7 +70,7 @@
wait_timeout: 600
with_items: "{{ start_asgs }}"
vars:
start_asgs: "{{ start_asg_facts | json_query('results[].{ name: auto_scaling_group_name, lc: launch_configuration_name}') }}"
start_asgs: "{{ start_asg_facts | json_query('results[].{ name: auto_scaling_group_name, lc: launch_configuration_name}') }}"

vars:
start: yes
Expand Down
8 changes: 4 additions & 4 deletions site.maintenance.aws-stop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
with_items: "{{ stop_asgs }}"
vars:
stop_asgs: "{{ stop_asg_facts | json_query('results[].{ name: auto_scaling_group_name, lc: launch_configuration_name}') }}"

- name: Stop standalone EC2 instances
when: stop_ec2
block:
- name: Get list of EC2 instances
ec2_instance_facts:
ec2_instance_info:
region: "{{ aws_region }}"
filters:
"vpc-id": "{{ aws_vpc_id }}"
register: stop_ec2_instance_facts
register: stop_ec2_instance_info

- name: Stop instances
ec2:
Expand All @@ -45,7 +45,7 @@
state: stopped
wait: no
vars:
stop_ec2_instance_ids: "{{ stop_ec2_instance_facts | json_query('instances[].instance_id') }}"
stop_ec2_instance_ids: "{{ stop_ec2_instance_info | json_query('instances[].instance_id') }}"
vars:
stop: yes
stop_ec2: "{{ stop }}"
Expand Down
4 changes: 2 additions & 2 deletions site.maintenance.blackfire.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
become: no
tasks:
- name: Get list of EC2 instances
ec2_instance_facts:
ec2_instance_info:
region: "{{ aws_region }}"
filters:
"tag:Role": "app"
Expand Down Expand Up @@ -56,4 +56,4 @@
- vars/aws/env.yml
vars:
blackfire_urls:
- "{{ magento_base_url }}"
- "{{ magento_base_url }}"

0 comments on commit 10789d8

Please sign in to comment.