summaryrefslogtreecommitdiffstats
path: root/awx_collection/tests/integration/targets/group/tasks/main.yml
blob: c9a492911c8ac46048b10247b7dd21bd2bf93d4d (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
---
- name: Generate a test ID
  set_fact:
    test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
  when: test_id is not defined

- name: Generate names
  set_fact:
    group_name1: "AWX-Collection-tests-group-group1-{{ test_id }}"
    group_name2: "AWX-Collection-tests-group-group2-{{ test_id }}"
    group_name3: "AWX-Collection-tests-group-group3-{{ test_id }}"
    group_name4: "AWX-Collection-tests-group-group4-{{ test_id }}"
    inv_name: "AWX-Collection-tests-group-inv-{{ test_id }}"
    host_name1: "AWX-Collection-tests-group-host1-{{ test_id }}"
    host_name2: "AWX-Collection-tests-group-host2-{{ test_id }}"
    host_name3: "AWX-Collection-tests-group-host3-{{ test_id }}"
    host_name4: "AWX-Collection-tests-group-host4-{{ test_id }}"

- name: Create an Inventory
  inventory:
    name: "{{ inv_name }}"
    organization: Default
    state: present
  register: inv_result

- name: Create a Host
  host:
    name: "{{ host_name4 }}"
    inventory: "{{ inv_name }}"
    state: present
  register: host_result

- name: Add Host to Group
  group:
    name: "{{ group_name1 }}"
    inventory: "{{ inv_name }}"
    hosts:
      - "{{ host_name4 }}"
    preserve_existing_hosts: true
  register: group_result

- assert:
    that:
      - inv_result is changed
      - host_result is changed
      - group_result is changed

- name: Create Group 1
  group:
    name: "{{ group_name1 }}"
    inventory: "{{ inv_result.id }}"
    state: present
    variables:
      foo: bar
  register: result

- assert:
    that:
      - "result is changed"

- name: Create Group 1 with exists
  group:
    name: "{{ group_name1 }}"
    inventory: "{{ inv_name }}"
    state: exists
    variables:
      foo: bar
  register: result

- assert:
    that:
      - "result is not changed"

- name: Delete Group 1
  group:
    name: "{{ group_name1 }}"
    inventory: "{{ inv_name }}"
    state: absent
    variables:
      foo: bar
  register: result

- assert:
    that:
      - "result is changed"

- name: Create Group 1 with exists
  group:
    name: "{{ group_name1 }}"
    inventory: "{{ inv_name }}"
    state: exists
    variables:
      foo: bar
  register: result

- assert:
    that:
      - "result is changed"

- name: Create Group 2
  group:
    name: "{{ group_name2 }}"
    inventory: "{{ inv_name }}"
    state: present
    variables:
      foo: bar
  register: result

- assert:
    that:
      - "result is changed"

- name: Create Group 3
  group:
    name: "{{ group_name3 }}"
    inventory: "{{ inv_name }}"
    state: present
    variables:
      foo: bar
  register: result

- assert:
    that:
      - "result is changed"

- name: add hosts
  host:
    name: "{{ item }}"
    inventory: "{{ inv_name }}"
  loop:
    - "{{ host_name1 }}"
    - "{{ host_name2 }}"
    - "{{ host_name3 }}"

- name: Create Group 1 with hosts and sub group of Group 2
  group:
    name: "{{ group_name1 }}"
    inventory: "{{ inv_name }}"
    hosts:
      - "{{ host_name1 }}"
      - "{{ host_name2 }}"
    children:
      - "{{ group_name2 }}"
    state: present
    variables:
      foo: bar
  register: result

- name: Create Group 1 with hosts and sub group
  group:
    name: "{{ group_name1 }}"
    inventory: "{{ inv_name }}"
    hosts:
      - "{{ host_name3 }}"
    children:
      - "{{ group_name3 }}"
    state: present
    preserve_existing_hosts: true
    preserve_existing_children: true
  register: result

- name: "Find number of hosts in {{ group_name1 }}"
  set_fact:
    group1_host_count: "{{ lookup('awx.awx.controller_api', 'groups/{{result.id}}/all_hosts/') |length}}"

- assert:
    that:
      - group1_host_count  == "3"

- name: Delete Group 3
  group:
    name: "{{ group_name3 }}"
    inventory: "{{ inv_name }}"
    state: absent
  register: result

- assert:
    that:
      - "result is changed"

# If we delete group 1 first it will delete group 2 and 3
- name: Delete Group 1
  group:
    name: "{{ group_name1 }}"
    inventory: "{{ inv_name }}"
    state: absent
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete Group 2
  group:
    name: "{{ group_name2 }}"
    inventory: "{{ inv_name }}"
    state: absent
  register: result

# In this case, group 2 was last a child of group1 so deleting group1 deleted group2
- assert:
    that:
      - "result is not changed"

- name: Check module fails with correct msg
  group:
    name: test-group
    description: Group Description
    inventory: test-non-existing-inventory
    state: present
  register: result
  ignore_errors: true

- assert:
    that:
      - "result is failed"
      - "result is not changed"
      - "'test-non-existing-inventory' in result.msg"
      - "result.total_results == 0"

- name: add hosts
  host:
    name: "{{ item }}"
    inventory: "{{ inv_name }}"
  loop:
    - "{{ host_name1 }}"
    - "{{ host_name2 }}"
    - "{{ host_name3 }}"

- name: add mid level group
  group:
    name: "{{ group_name2 }}"
    inventory: "{{ inv_name }}"
    hosts:
      - "{{ host_name3 }}"

- name: add top group
  group:
    name: "{{ group_name3 }}"
    inventory: "{{ inv_name }}"
    hosts:
      - "{{ host_name1 }}"
      - "{{ host_name2 }}"
    children:
      - "{{ group_name2 }}"

- name: Delete the parent group
  group:
    name: "{{ group_name3 }}"
    inventory: "{{ inv_name }}"
    state: absent

- name: Delete the child group
  group:
    name: "{{ group_name2 }}"
    inventory: "{{ inv_name }}"
    state: absent

- name: Delete an Inventory
  inventory:
    name: "{{ inv_name }}"
    organization: Default
    state: absent