summaryrefslogtreecommitdiff
path: root/test/integration/targets/ec2_instance/roles/ec2_instance/tasks/tags_and_vpc_settings.yml
blob: d38b53f76fb97675f5657b59c6c6d9833a6fa6d3 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
- block:
  - name: "Make instance in the testing subnet created in the test VPC"
    ec2_instance:
      state: present
      name: "{{ resource_prefix }}-test-basic-vpc-create"
      image_id: "{{ ec2_ami_image }}"
      user_data: |
        #cloud-config
        package_upgrade: true
        package_update: true
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
        Something: else
      security_groups: "{{ sg.group_id }}"
      network:
        source_dest_check: false
      vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
      instance_type: "{{ ec2_instance_type }}"
      wait: false
    register: in_test_vpc

  - name: "Make instance in the testing subnet created in the test VPC(check mode)"
    ec2_instance:
      state: present
      name: "{{ resource_prefix }}-test-basic-vpc-create-checkmode"
      image_id: "{{ ec2_ami_image }}"
      user_data: |
        #cloud-config
        package_upgrade: true
        package_update: true
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
        Something: else
      security_groups: "{{ sg.group_id }}"
      network:
        source_dest_check: false
      vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
      instance_type: "{{ ec2_instance_type }}"
    check_mode: yes

  - name: "Try to re-make the instance, hopefully this shows changed=False"
    ec2_instance:
      state: present
      name: "{{ resource_prefix }}-test-basic-vpc-create"
      image_id: "{{ ec2_ami_image }}"
      user_data: |
        #cloud-config
        package_upgrade: true
        package_update: true
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
        Something: else
      security_groups: "{{ sg.group_id }}"
      vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
      instance_type: "{{ ec2_instance_type }}"
    register: remake_in_test_vpc
  - name: "Remaking the same instance resulted in no changes"
    assert:
      that: not remake_in_test_vpc.changed
  - name: "check that instance IDs match anyway"
    assert:
      that: 'remake_in_test_vpc.instance_ids[0] == in_test_vpc.instance_ids[0]'
  - name: "check that source_dest_check was set to false"
    assert:
      that: 'not remake_in_test_vpc.instances[0].source_dest_check'

  - name: "fact presented ec2 instance"
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-test-basic-vpc-create"
    register: presented_instance_fact

  - name: "fact checkmode ec2 instance"
    ec2_instance_info:
      filters:
        "tag:Name": "{{ resource_prefix }}-test-basic-vpc-create-checkmode"
    register: checkmode_instance_fact

  - name: "Confirm whether the check mode is working normally."
    assert:
      that:
        - "{{ presented_instance_fact.instances | length }} > 0"
        - "{{ checkmode_instance_fact.instances | length }} == 0"

  - name: "Alter it by adding tags"
    ec2_instance:
      state: present
      name: "{{ resource_prefix }}-test-basic-vpc-create"
      image_id: "{{ ec2_ami_image }}"
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
        Another: thing
      security_groups: "{{ sg.group_id }}"
      vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
      instance_type: "{{ ec2_instance_type }}"
    register: add_another_tag

  - ec2_instance_info:
      instance_ids: "{{ add_another_tag.instance_ids }}"
    register: check_tags
  - name: "Remaking the same instance resulted in no changes"
    assert:
      that:
        - check_tags.instances[0].tags.Another == 'thing'
        - check_tags.instances[0].tags.Something == 'else'

  - name: "Purge a tag"
    ec2_instance:
      state: present
      name: "{{ resource_prefix }}-test-basic-vpc-create"
      image_id: "{{ ec2_ami_image }}"
      purge_tags: true
      tags:
        TestId: "{{ ec2_instance_tag_TestId }}"
        Another: thing
      security_groups: "{{ sg.group_id }}"
      vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
      instance_type: "{{ ec2_instance_type }}"

  - ec2_instance_info:
      instance_ids: "{{ add_another_tag.instance_ids }}"
    register: check_tags

  - name: "Remaking the same instance resulted in no changes"
    assert:
      that:
        - "'Something' not in check_tags.instances[0].tags"

  - name: "check that subnet-default public IP rule was followed"
    assert:
      that:
        - check_tags.instances[0].public_dns_name == ""
        - check_tags.instances[0].private_ip_address.startswith(subnet_b_startswith)
        - check_tags.instances[0].subnet_id == testing_subnet_b.subnet.id
  - name: "check that tags were applied"
    assert:
      that:
        - check_tags.instances[0].tags.Name.startswith(resource_prefix)
        - "'{{ check_tags.instances[0].state.name }}' in  ['pending', 'running']"

  - name: "Terminate instance"
    ec2_instance:
      state: absent
      filters:
        "tag:TestId": "{{ ec2_instance_tag_TestId }}"
      wait: false
    register: result
  - assert:
      that: result.changed

  always:
  - name: "Terminate tags_and_vpc_settings instances"
    ec2_instance:
      state: absent
      filters:
        "tag:TestId": "{{ ec2_instance_tag_TestId }}"
      wait: yes
    ignore_errors: yes