Guillaume Coré
2018-11-14 1c72dabdaf626a7d0d84fd13f5de3bd74e0a15a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
---
- name: Get all custom AMI for that environment / version
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
    AWS_DEFAULT_REGION: "{{aws_region_loop|d(aws_region)}}"
  ec2_ami_facts:
    owner: self
    filters:
      tag:env_type: "{{ env_type }}"
      tag:version: "{{ osrelease }}"
      tag:stages: "*{{ custom_image_stage | d('unknown') }}*"
  register: amifacts
 
# by default do not skip docker tasks
- set_fact:
    skip_packer_tasks: false
 
- when: amifacts.images | length > 0
  block:
    # For now, use one image for all the machines of the config. Start simple.
    # TODO: add another tag to the image to apply it to specific group of hosts in the config
    - set_fact:
        custom_image: "{{ amifacts | json_query('images[]') | sort(attribute='creation_date') | last }}"
 
    - name: Print debug information about the custom image used
      debug:
        var: custom_image
 
    - name: Skip packer tasks if the tags in the image say to do so
      set_fact:
        skip_packer_tasks: true
      when:
        - custom_image is defined
        - custom_image.tags.skip_packer_tasks is defined
        - custom_image.tags.skip_packer_tasks in ['true', 'yes', 'True', 'Yes']