| 1 |
--- |
| 2 |
- block: |
| 3 |
# Ensure profile doesn't already exist (from an old test) |
| 4 |
- name: Delete Instance Profile |
| 5 |
amazon.aws.iam_instance_profile: |
| 6 |
state: absent |
| 7 |
name: "{{ test_role }}" |
| 8 |
|
| 9 |
################################################################## |
| 10 |
|
| 11 |
# Profile doesn't exist, don't create |
| 12 |
- name: Minimal IAM Role without instance profile (without existing profile) |
| 13 |
amazon.aws.iam_role: |
| 14 |
name: "{{ test_role }}" |
| 15 |
create_instance_profile: false |
| 16 |
register: iam_role |
| 17 |
- ansible.builtin.assert: |
| 18 |
that: |
| 19 |
- iam_role is changed |
| 20 |
- '"iam:CreateInstanceProfile" not in iam_role.resource_actions' |
| 21 |
|
| 22 |
- name: Verify instance profile doesn't exist |
| 23 |
amazon.aws.iam_instance_profile_info: |
| 24 |
name: "{{ test_role }}" |
| 25 |
register: iam_instance_profile |
| 26 |
- ansible.builtin.assert: |
| 27 |
that: |
| 28 |
- iam_instance_profile.iam_instance_profiles | length == 0 |
| 29 |
|
| 30 |
# Profile doesn't exist, do delete |
| 31 |
- name: Remove IAM Role and profile (with non-existent profile) |
| 32 |
amazon.aws.iam_role: |
| 33 |
state: absent |
| 34 |
name: "{{ test_role }}" |
| 35 |
delete_instance_profile: true |
| 36 |
register: iam_role |
| 37 |
- ansible.builtin.assert: |
| 38 |
that: |
| 39 |
- iam_role is changed |
| 40 |
- '"iam:DeleteInstanceProfile" not in iam_role.resource_actions' |
| 41 |
|
| 42 |
- name: Verify instance profile doesn't exist |
| 43 |
amazon.aws.iam_instance_profile_info: |
| 44 |
name: "{{ test_role }}" |
| 45 |
register: iam_instance_profile |
| 46 |
- ansible.builtin.assert: |
| 47 |
that: |
| 48 |
- iam_instance_profile.iam_instance_profiles | length == 0 |
| 49 |
|
| 50 |
################################################################## |
| 51 |
|
| 52 |
# Profile doesn't exist, do create |
| 53 |
- name: Minimal IAM Role with instance profile (without existing profile) |
| 54 |
amazon.aws.iam_role: |
| 55 |
name: "{{ test_role }}" |
| 56 |
create_instance_profile: true |
| 57 |
register: iam_role |
| 58 |
- ansible.builtin.assert: |
| 59 |
that: |
| 60 |
- iam_role is changed |
| 61 |
- '"iam:CreateInstanceProfile" in iam_role.resource_actions' |
| 62 |
|
| 63 |
- name: Verify instance profile exists |
| 64 |
amazon.aws.iam_instance_profile_info: |
| 65 |
name: "{{ test_role }}" |
| 66 |
register: iam_instance_profile |
| 67 |
- ansible.builtin.assert: |
| 68 |
that: |
| 69 |
- iam_instance_profile.iam_instance_profiles | length == 1 |
| 70 |
|
| 71 |
# Profile does exist, don't delete |
| 72 |
- name: Remove IAM Role and not profile (with existent profile) |
| 73 |
amazon.aws.iam_role: |
| 74 |
state: absent |
| 75 |
name: "{{ test_role }}" |
| 76 |
delete_instance_profile: false |
| 77 |
register: iam_role |
| 78 |
- ansible.builtin.assert: |
| 79 |
that: |
| 80 |
- iam_role is changed |
| 81 |
- '"iam:DeleteInstanceProfile" not in iam_role.resource_actions' |
| 82 |
|
| 83 |
- name: Verify instance profile exists |
| 84 |
amazon.aws.iam_instance_profile_info: |
| 85 |
name: "{{ test_role }}" |
| 86 |
register: iam_instance_profile |
| 87 |
- ansible.builtin.assert: |
| 88 |
that: |
| 89 |
- iam_instance_profile.iam_instance_profiles | length == 1 |
| 90 |
|
| 91 |
################################################################## |
| 92 |
|
| 93 |
# Profile does exist, do create |
| 94 |
- name: Minimal IAM Role with instance profile (profile exists) |
| 95 |
amazon.aws.iam_role: |
| 96 |
name: "{{ test_role }}" |
| 97 |
create_instance_profile: true |
| 98 |
register: iam_role |
| 99 |
- ansible.builtin.assert: |
| 100 |
that: |
| 101 |
- iam_role is changed |
| 102 |
- '"iam:CreateInstanceProfile" not in iam_role.resource_actions' |
| 103 |
|
| 104 |
- name: Verify instance profile exists |
| 105 |
amazon.aws.iam_instance_profile_info: |
| 106 |
name: "{{ test_role }}" |
| 107 |
register: iam_instance_profile |
| 108 |
- ansible.builtin.assert: |
| 109 |
that: |
| 110 |
- iam_instance_profile.iam_instance_profiles | length == 1 |
| 111 |
|
| 112 |
# Profile does exist, don't delete |
| 113 |
- name: Remove IAM Role and don't delete profile (with existent profile) |
| 114 |
amazon.aws.iam_role: |
| 115 |
state: absent |
| 116 |
name: "{{ test_role }}" |
| 117 |
delete_instance_profile: false |
| 118 |
register: iam_role |
| 119 |
- ansible.builtin.assert: |
| 120 |
that: |
| 121 |
- iam_role is changed |
| 122 |
- '"iam:DeleteInstanceProfile" not in iam_role.resource_actions' |
| 123 |
|
| 124 |
- name: Verify instance profile exists |
| 125 |
amazon.aws.iam_instance_profile_info: |
| 126 |
name: "{{ test_role }}" |
| 127 |
register: iam_instance_profile |
| 128 |
- ansible.builtin.assert: |
| 129 |
that: |
| 130 |
- iam_instance_profile.iam_instance_profiles | length == 1 |
| 131 |
|
| 132 |
################################################################## |
| 133 |
# No create profile - profile already exists |
| 134 |
|
| 135 |
# Profile does exist, don't create |
| 136 |
- name: Minimal IAM Role without instance profile (profile exists) |
| 137 |
amazon.aws.iam_role: |
| 138 |
name: "{{ test_role }}" |
| 139 |
create_instance_profile: false |
| 140 |
register: iam_role |
| 141 |
|
| 142 |
- ansible.builtin.assert: |
| 143 |
that: |
| 144 |
- iam_role is changed |
| 145 |
- '"iam:CreateInstanceProfile" not in iam_role.resource_actions' |
| 146 |
|
| 147 |
- name: Verify instance profile exists |
| 148 |
amazon.aws.iam_instance_profile_info: |
| 149 |
name: "{{ test_role }}" |
| 150 |
register: iam_instance_profile |
| 151 |
|
| 152 |
- ansible.builtin.assert: |
| 153 |
that: |
| 154 |
- iam_instance_profile.iam_instance_profiles | length == 1 |
| 155 |
|
| 156 |
- name: Attach Role to profile |
| 157 |
amazon.aws.iam_instance_profile: |
| 158 |
name: "{{ test_role }}" |
| 159 |
role: "{{ test_role }}" |
| 160 |
register: iam_instance_profile |
| 161 |
|
| 162 |
- ansible.builtin.assert: |
| 163 |
that: |
| 164 |
- iam_instance_profile is changed |
| 165 |
|
| 166 |
# Profile does exist, do delete |
| 167 |
- name: Remove IAM Role and delete profile (with existent profile) |
| 168 |
amazon.aws.iam_role: |
| 169 |
state: absent |
| 170 |
name: "{{ test_role }}" |
| 171 |
delete_instance_profile: true |
| 172 |
register: iam_role |
| 173 |
|
| 174 |
- ansible.builtin.assert: |
| 175 |
that: |
| 176 |
- iam_role is changed |
| 177 |
- '"iam:DeleteInstanceProfile" in iam_role.resource_actions' |
| 178 |
|
| 179 |
- name: Verify instance profile exists |
| 180 |
amazon.aws.iam_instance_profile_info: |
| 181 |
name: "{{ test_role }}" |
| 182 |
register: iam_instance_profile |
| 183 |
|
| 184 |
- ansible.builtin.assert: |
| 185 |
that: |
| 186 |
- iam_instance_profile.iam_instance_profiles | length == 0 |
| 187 |
|
| 188 |
################################################################## |
| 189 |
|
| 190 |
# Profile doesn't exist, don't create |
| 191 |
- name: Minimal IAM Role without instance profile (without existing profile) |
| 192 |
amazon.aws.iam_role: |
| 193 |
name: "{{ test_role }}" |
| 194 |
create_instance_profile: false |
| 195 |
register: iam_role |
| 196 |
- ansible.builtin.assert: |
| 197 |
that: |
| 198 |
- iam_role is changed |
| 199 |
- '"iam:CreateInstanceProfile" not in iam_role.resource_actions' |
| 200 |
|
| 201 |
- name: Verify instance profile doesn't exist |
| 202 |
amazon.aws.iam_instance_profile_info: |
| 203 |
name: "{{ test_role }}" |
| 204 |
register: iam_instance_profile |
| 205 |
- ansible.builtin.assert: |
| 206 |
that: |
| 207 |
- iam_instance_profile.iam_instance_profiles | length == 0 |
| 208 |
|
| 209 |
# Profile doesn't exist, don't delete |
| 210 |
- name: Remove IAM Role and profile (with non-existent profile) |
| 211 |
amazon.aws.iam_role: |
| 212 |
state: absent |
| 213 |
name: "{{ test_role }}" |
| 214 |
delete_instance_profile: false |
| 215 |
register: iam_role |
| 216 |
- ansible.builtin.assert: |
| 217 |
that: |
| 218 |
- iam_role is changed |
| 219 |
- '"iam:DeleteInstanceProfile" not in iam_role.resource_actions' |
| 220 |
|
| 221 |
- name: Verify instance profile doesn't exist |
| 222 |
amazon.aws.iam_instance_profile_info: |
| 223 |
name: "{{ test_role }}" |
| 224 |
register: iam_instance_profile |
| 225 |
- ansible.builtin.assert: |
| 226 |
that: |
| 227 |
- iam_instance_profile.iam_instance_profiles | length == 0 |
| 228 |
|
| 229 |
################################################################## |
| 230 |
|
| 231 |
# Profile doesn't exist, do create |
| 232 |
- name: Minimal IAM Role with instance profile (profile does not exist) |
| 233 |
amazon.aws.iam_role: |
| 234 |
name: "{{ test_role }}" |
| 235 |
create_instance_profile: true |
| 236 |
register: iam_role |
| 237 |
- ansible.builtin.assert: |
| 238 |
that: |
| 239 |
- iam_role is changed |
| 240 |
- '"iam:CreateInstanceProfile" in iam_role.resource_actions' |
| 241 |
|
| 242 |
- name: Decouple Instance Profile from role |
| 243 |
amazon.aws.iam_instance_profile: |
| 244 |
name: "{{ test_role }}" |
| 245 |
role: "" |
| 246 |
register: iam_instance_profile |
| 247 |
|
| 248 |
- ansible.builtin.assert: |
| 249 |
that: |
| 250 |
- iam_instance_profile is changed |
| 251 |
|
| 252 |
# Detached profile exists, we shouldn't delete it. |
| 253 |
- name: Remove IAM Role and "delete" profile (with detached profile) |
| 254 |
amazon.aws.iam_role: |
| 255 |
state: absent |
| 256 |
name: "{{ test_role }}" |
| 257 |
delete_instance_profile: true |
| 258 |
register: iam_role |
| 259 |
- ansible.builtin.assert: |
| 260 |
that: |
| 261 |
- iam_role is changed |
| 262 |
- '"iam:DeleteInstanceProfile" not in iam_role.resource_actions' |
| 263 |
|
| 264 |
- name: Verify instance profile exists |
| 265 |
amazon.aws.iam_instance_profile_info: |
| 266 |
name: "{{ test_role }}" |
| 267 |
register: iam_instance_profile |
| 268 |
- ansible.builtin.assert: |
| 269 |
that: |
| 270 |
- iam_instance_profile.iam_instance_profiles | length == 1 |
| 271 |
|
| 272 |
################################################################## |
| 273 |
# Delete profile |
| 274 |
|
| 275 |
- name: Delete Instance Profile |
| 276 |
amazon.aws.iam_instance_profile: |
| 277 |
state: absent |
| 278 |
name: "{{ test_role }}" |
| 279 |
register: iam_instance_profile |
| 280 |
|
| 281 |
- ansible.builtin.assert: |
| 282 |
that: |
| 283 |
- iam_instance_profile is changed |
| 284 |
|
| 285 |
always: |
| 286 |
- name: Delete Instance Profiles |
| 287 |
amazon.aws.iam_instance_profile: |
| 288 |
state: absent |
| 289 |
name: "{{ test_role }}" |
| 290 |
ignore_errors: true |