summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/gathering_facts/test_gathering_facts.yml
blob: 47027e87175d679fef9ef935ec96718a2a092c8e (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
---
- hosts: facthost7
  tags: [ 'fact_negation' ]
  connection: local
  gather_subset: "!hardware"
  gather_facts: no
  tasks:
    - name: setup with not hardware
      setup:
       gather_subset:
           - "!hardware"
      register: not_hardware_facts

- name: min and network test for platform added
  hosts: facthost21
  tags: [ 'fact_network' ]
  connection: local
  gather_subset: ["!all", "network"]
  gather_facts: yes
  tasks:
    - name: Test that retrieving network facts works and gets prereqs from platform and distribution
      assert:
        that:
          - 'ansible_default_ipv4|default("UNDEF") != "UNDEF"'
          - 'ansible_interfaces|default("UNDEF") != "UNDEF"'
            # these are true for linux, but maybe not for other os
          - 'ansible_system|default("UNDEF") != "UNDEF"'
          - 'ansible_distribution|default("UNDEF") != "UNDEF"'
            # we dont really require these but they are in the min set
            # - 'ansible_virtualization_role|default("UNDEF") == "UNDEF"'
            # - 'ansible_user_id|default("UNDEF") == "UNDEF"'
            # - 'ansible_env|default("UNDEF") == "UNDEF"'
            # - 'ansible_selinux|default("UNDEF") == "UNDEF"'
            # - 'ansible_pkg_mgr|default("UNDEF") == "UNDEF"'

- name: min and hardware test for platform added
  hosts: facthost22
  tags: [ 'fact_hardware' ]
  connection: local
  gather_subset: "hardware"
  gather_facts: yes
  tasks:
    - name: debug stuff
      debug:
        var: hostvars['facthost22']
    # we should also collect platform, but not distribution
    - name: Test that retrieving hardware facts works and gets prereqs from platform and distribution
      when: ansible_system|default("UNDEF") == "Linux"
      assert:
        # LinuxHardwareCollector requires 'platform' facts
        that:
          - 'ansible_memory_mb|default("UNDEF") != "UNDEF"'
          - 'ansible_default_ipv4|default("UNDEF") == "UNDEF"'
          - 'ansible_interfaces|default("UNDEF") == "UNDEF"'
            # these are true for linux, but maybe not for other os
            # hardware requires 'platform'
          - 'ansible_system|default("UNDEF") != "UNDEF"'
          - 'ansible_machine|default("UNDEF") != "UNDEF"'
            # hardware does not require 'distribution' but it is min set
            # - 'ansible_distribution|default("UNDEF") == "UNDEF"'
            # we dont really require these but they are in the min set
            # - 'ansible_virtualization_role|default("UNDEF") == "UNDEF"'
            # - 'ansible_user_id|default("UNDEF") == "UNDEF"'
            # - 'ansible_env|default("UNDEF") == "UNDEF"'
            # - 'ansible_selinux|default("UNDEF") == "UNDEF"'
            # - 'ansible_pkg_mgr|default("UNDEF") == "UNDEF"'

- name: min and service_mgr test for platform added
  hosts: facthost23
  tags: [ 'fact_service_mgr' ]
  connection: local
  gather_subset: ["!all", "service_mgr"]
  gather_facts: yes
  tasks:
    - name: Test that retrieving service_mgr facts works and gets prereqs from platform and distribution
      assert:
        that:
          - 'ansible_service_mgr|default("UNDEF") != "UNDEF"'
          - 'ansible_default_ipv4|default("UNDEF") == "UNDEF"'
          - 'ansible_interfaces|default("UNDEF") == "UNDEF"'
            # these are true for linux, but maybe not for other os
          - 'ansible_system|default("UNDEF") != "UNDEF"'
          - 'ansible_distribution|default("UNDEF") != "UNDEF"'
            # we dont really require these but they are in the min set
            # - 'ansible_virtualization_role|default("UNDEF") == "UNDEF"'
            # - 'ansible_user_id|default("UNDEF") == "UNDEF"'
            # - 'ansible_env|default("UNDEF") == "UNDEF"'
            # - 'ansible_selinux|default("UNDEF") == "UNDEF"'
            # - 'ansible_pkg_mgr|default("UNDEF") == "UNDEF"'

- hosts: facthost0
  tags: [ 'fact_min' ]
  connection: local
  gather_subset: "all"
  gather_facts: yes
  tasks:
    #- setup:
    #  register: facts
    - name: Test that retrieving all facts works
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_interfaces|default("UNDEF_NET") != "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") != "UNDEF_MOUNT" or ansible_distribution == "MacOSX"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") != "UNDEF_VIRT"'

- hosts: facthost1
  tags: [ 'fact_min' ]
  connection: local
  gather_facts: no
  tasks:
    - name: Test that we can retrieve the loadavg fact
      setup:
        filter: "ansible_loadavg"

    - name: Test the contents of the loadavg fact
      assert:
        that:
          - 'ansible_loadavg|default("UNDEF_ENV") != "UNDEF_ENV"'
          - '"1m" in ansible_loadavg and ansible_loadavg["1m"] is float'
          - '"5m" in ansible_loadavg and ansible_loadavg["5m"] is float'
          - '"15m" in ansible_loadavg and ansible_loadavg["15m"] is float'

- hosts: facthost19
  tags: [ 'fact_min' ]
  connection: local
  gather_facts: no
  tasks:
    - setup:
        filter: "*env*"
        # register: fact_results

    - name: Test that retrieving all facts filtered to env works
      assert:
        that:
          - 'ansible_interfaces|default("UNDEF_NET") == "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") == "UNDEF_VIRT"'
          - 'ansible_env|default("UNDEF_ENV") != "UNDEF_ENV"'

- hosts: facthost24
  tags: [ 'fact_min' ]
  connection: local
  gather_facts: no
  tasks:
    - setup:
        filter:
            - "*env*"
            - "*virt*"

    - name: Test that retrieving all facts filtered to env and virt works
      assert:
        that:
          - 'ansible_interfaces|default("UNDEF_NET") == "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") != "UNDEF_VIRT"'
          - 'ansible_env|default("UNDEF_ENV") != "UNDEF_ENV"'

- hosts: facthost25
  tags: [ 'fact_min' ]
  gather_facts: no
  tasks:
    - setup:
        filter:
            - "date_time"

    - name: Test that retrieving all facts filtered to date_time even w/o using ansible_ prefix
      assert:
        that:
          - 'ansible_facts["date_time"]|default("UNDEF_MOUNT") != "UNDEF_MOUNT"'
          - 'ansible_date_time|default("UNDEF_MOUNT") != "UNDEF_MOUNT"'

- hosts: facthost26
  tags: [ 'fact_min' ]
  gather_facts: no
  tasks:
    - setup:
        filter:
            - "ansible_date_time"

    - name: Test that retrieving all facts filtered to date_time even using ansible_ prefix
      assert:
        that:
          - 'ansible_facts["date_time"]|default("UNDEF_MOUNT") != "UNDEF_MOUNT"'
          - 'ansible_date_time|default("UNDEF_MOUNT") != "UNDEF_MOUNT"'

- hosts: facthost13
  tags: [ 'fact_min' ]
  connection: local
  gather_facts: no
  tasks:
    - setup:
        filter: "ansible_user_id"
        # register: fact_results

    - name: Test that retrieving all facts filtered to specific fact ansible_user_id works
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_USER") != "UNDEF_USER"'
          - 'ansible_interfaces|default("UNDEF_NET") == "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") == "UNDEF_VIRT"'
          - 'ansible_env|default("UNDEF_ENV") == "UNDEF_ENV"'
          - 'ansible_pkg_mgr|default("UNDEF_PKG_MGR") == "UNDEF_PKG_MGR"'

- hosts: facthost11
  tags: [ 'fact_min' ]
  connection: local
  gather_facts: no
  tasks:
    - setup:
        filter: "*"
        # register: fact_results

    - name: Test that retrieving all facts filtered to splat
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_interfaces|default("UNDEF_NET") != "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") != "UNDEF_MOUNT" or ansible_distribution == "MacOSX"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") != "UNDEF_VIRT"'

- hosts: facthost12
  tags: [ 'fact_min' ]
  connection: local
  gather_facts: no
  tasks:
    - setup:
        filter: ""
        # register: fact_results

    - name: Test that retrieving all facts filtered to empty filter_spec works
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_interfaces|default("UNDEF_NET") != "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") != "UNDEF_MOUNT" or ansible_distribution == "MacOSX"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") != "UNDEF_VIRT"'

- hosts: facthost1
  tags: [ 'fact_min' ]
  connection: local
  gather_subset: "!all"
  gather_facts: yes
  tasks:
    - name: Test that only retrieving minimal facts work
      assert:
        that:
          # from the min set, which should still collect
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_env|default("UNDEF_ENV") != "UNDEF_ENV"'
          # non min facts that are not collected
          - 'ansible_interfaces|default("UNDEF_NET") == "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") == "UNDEF_VIRT"'

- hosts: facthost2
  tags: [ 'fact_network' ]
  connection: local
  gather_subset: ["!all", "!min", "network"]
  gather_facts: yes
  tasks:
    - name: Test that retrieving network facts work
      assert:
        that:
          - 'ansible_user_id|default("UNDEF") == "UNDEF"'
          - 'ansible_interfaces|default("UNDEF_NET") != "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF") == "UNDEF"'
          - 'ansible_virtualization_role|default("UNDEF") == "UNDEF"'

- hosts: facthost3
  tags: [ 'fact_hardware' ]
  connection: local
  gather_subset: "hardware"
  gather_facts: yes
  tasks:
    - name: Test that retrieving hardware facts work
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_interfaces|default("UNDEF_NET") == "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") != "UNDEF_MOUNT" or ansible_distribution == "MacOSX"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") == "UNDEF_VIRT"'

- hosts: facthost4
  tags: [ 'fact_virtual' ]
  connection: local
  gather_subset: "virtual"
  gather_facts: yes
  tasks:
    - name: Test that retrieving virtualization facts work
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_interfaces|default("UNDEF_NET") == "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") != "UNDEF_VIRT"'

- hosts: facthost5
  tags: [ 'fact_comma_string' ]
  connection: local
  gather_subset: ["virtual", "network"]
  gather_facts: yes
  tasks:
    - name: Test that retrieving virtualization and network as a string works
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_interfaces|default("UNDEF_NET") != "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") != "UNDEF_VIRT"'

- hosts: facthost6
  tags: [ 'fact_yaml_list' ]
  connection: local
  gather_subset:
    - virtual
    - network
  gather_facts: yes
  tasks:
    - name: Test that retrieving virtualization and network as a string works
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_interfaces|default("UNDEF_NET") != "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") != "UNDEF_VIRT"'


- hosts: facthost7
  tags: [ 'fact_negation' ]
  connection: local
  gather_subset: "!hardware"
  gather_facts: yes
  tasks:
    - name: Test that negation of fact subsets work
      assert:
        that:
          # network, not collected since it is not in min
          - 'ansible_interfaces|default("UNDEF_NET") == "UNDEF_NET"'
          # not collecting virt, should be undef
          - 'ansible_virtualization_role|default("UNDEF_VIRT") == "UNDEF_VIRT"'
          # mounts/devices are collected by hardware, so should be not collected and undef
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_devices|default("UNDEF_DEVICES") == "UNDEF_DEVICES"'
          # from the min set, which should still collect
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_env|default("UNDEF_ENV") != "UNDEF_ENV"'

- hosts: facthost8
  tags: [ 'fact_mixed_negation_addition' ]
  connection: local
  gather_subset: ["!hardware", "network"]
  gather_facts: yes
  tasks:
    - name: Test that negation and additional subsets work together
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'
          - 'ansible_interfaces|default("UNDEF_NET") != "UNDEF_NET"'
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") == "UNDEF_VIRT"'

- hosts: facthost14
  tags: [ 'fact_mixed_negation_addition_min' ]
  connection: local
  gather_subset: ["!all", "!min", "network"]
  gather_facts: yes
  tasks:
    - name: Test that negation and additional subsets work together for min subset
      assert:
        that:
          - 'ansible_user_id|default("UNDEF_MIN") == "UNDEF_MIN"'
          - 'ansible_interfaces|default("UNDEF_NET") != "UNDEF_NET"'
          - 'ansible_default_ipv4|default("UNDEF_DEFAULT_IPV4") != "UNDEF_DEFAULT_IPV4"'
          - 'ansible_all_ipv4_addresses|default("UNDEF_ALL_IPV4") != "UNDEF_ALL_IPV4"'
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_virtualization_role|default("UNDEF_VIRT") == "UNDEF_VIRT"'
          - 'ansible_env|default("UNDEF_ENV") == "UNDEF_ENV"'

- hosts: facthost15
  tags: [ 'fact_negate_all_min_add_pkg_mgr' ]
  connection: local
  gather_subset: ["!all", "!min", "pkg_mgr"]
  gather_facts: yes
  tasks:
    - name: Test that negation and additional subsets work together for min subset
      assert:
        that:
          # network, not collected since it is not in min
          - 'ansible_interfaces|default("UNDEF_NET") == "UNDEF_NET"'
          # not collecting virt, should be undef
          - 'ansible_virtualization_role|default("UNDEF_VIRT") == "UNDEF_VIRT"'
          # mounts/devices are collected by hardware, so should be not collected and undef
          - 'ansible_mounts|default("UNDEF_MOUNT") == "UNDEF_MOUNT"'
          - 'ansible_devices|default("UNDEF_DEVICES") == "UNDEF_DEVICES"'
          # from the min set, which should not collect
          - 'ansible_user_id|default("UNDEF_MIN") == "UNDEF_MIN"'
          - 'ansible_env|default("UNDEF_ENV") == "UNDEF_ENV"'
          # the pkg_mgr fact we requested explicitly
          - 'ansible_pkg_mgr|default("UNDEF_PKG_MGR") != "UNDEF_PKG_MGR"'


- hosts: facthost9
  tags: [ 'fact_local']
  connection: local
  gather_facts: no
  tasks:
    - name: Create fact directories
      become: true
      with_items:
        - /etc/ansible/facts.d
        - /tmp/custom_facts.d
      file:
        state: directory
        path: "{{ item }}"
        mode: '0777'
    - name: Deploy local facts
      with_items:
        - path: /etc/ansible/facts.d/testfact.fact
          content: '{ "fact_dir": "default" }'
        - path: /tmp/custom_facts.d/testfact.fact
          content: '{ "fact_dir": "custom" }'
      copy:
        dest: "{{ item.path }}"
        content: "{{ item.content }}"

- hosts: facthost9
  tags: [ 'fact_local']
  connection: local
  gather_facts: yes
  tasks:
    - name: Test reading facts from default fact_path
      assert:
        that:
          - '"{{ ansible_local.testfact.fact_dir }}" == "default"'

- hosts: facthost9
  tags: [ 'fact_local']
  connection: local
  gather_facts: yes
  fact_path: /tmp/custom_facts.d
  tasks:
    - name: Test reading facts from custom fact_path
      assert:
        that:
          - '"{{ ansible_local.testfact.fact_dir }}" == "custom"'

- hosts: facthost20
  tags: [ 'fact_facter_ohai' ]
  connection: local
  gather_subset:
    - facter
    - ohai
  gather_facts: yes
  tasks:
    - name: Test that retrieving facter and ohai doesnt fail
      assert:
        # not much to assert here, aside from not crashing, since test images dont have
        # facter/ohai
        that:
          - 'ansible_user_id|default("UNDEF_MIN") != "UNDEF_MIN"'

- hosts: facthost9
  tags: [ 'fact_file_utils' ]
  connection: local
  gather_facts: false
  tasks:
    - block:
        - name: Ensure get_file_content works when strip=False
          file_utils:
            test: strip

        - assert:
            that:
              - ansible_facts.get('etc_passwd_newlines', 0) + 1 == ansible_facts.get('etc_passwd_newlines_unstripped', 0)

        - name: Make an empty file
          file:
            path: "{{ output_dir }}/empty_file"
            state: touch

        - name: Ensure get_file_content gives default when file is empty
          file_utils:
            test: default
            touch_file: "{{ output_dir }}/empty_file"

        - assert:
            that:
              - ansible_facts.get('touch_default') == 'i am a default'

        - copy:
            dest: "{{ output_dir }}/1charsep"
            content: "foo:bar:baz:buzz:"

        - copy:
            dest: "{{ output_dir }}/2charsep"
            content: "foo::bar::baz::buzz::"

        - name: Ensure get_file_lines works as expected with specified 1-char line_sep
          file_utils:
            test: line_sep
            line_sep_file: "{{ output_dir }}/1charsep"
            line_sep_sep: ":"

        - assert:
            that:
              - ansible_facts.get('line_sep') == ['foo', 'bar', 'baz', 'buzz']

        - name: Ensure get_file_lines works as expected with specified 1-char line_sep
          file_utils:
            test: line_sep
            line_sep_file: "{{ output_dir }}/2charsep"
            line_sep_sep: "::"

        - assert:
            that:
              - ansible_facts.get('line_sep') == ['foo', 'bar', 'baz', 'buzz', '']

        - name: Ensure get_mount_size fails gracefully
          file_utils:
            test: invalid_mountpoint

        - assert:
            that:
              - ansible_facts['invalid_mountpoint']|length == 0

      always:
        - name: Remove test files
          file:
            path: "{{ item }}"
            state: absent
          with_items:
            - "{{ output_dir }}/empty_file"
            - "{{ output_dir }}/1charsep"
            - "{{ output_dir }}/2charsep"