summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/hostname/tasks/main.yml
blob: 596dd89afe07e4a0eab10da82d5955a9e1a8f1ad (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
# Setting the hostname in our test containers doesn't work currently
- when: ansible_facts.virtualization_type not in ('docker', 'container', 'containerd')
  block:
    - name: Include distribution specific variables
      include_vars: "{{ lookup('first_found', params) }}"
      vars:
        params:
          files:
            - "{{ ansible_facts.distribution }}.yml"
            - "{{ ansible_facts.os_family }}.yml"
            - default.yml
          paths:
            - "{{ role_path }}/vars"

    - name: Get current hostname
      command: hostname
      register: original

    - import_tasks: test_check_mode.yml
    - import_tasks: test_normal.yml

    - name: Include distribution specific tasks
      include_tasks:
        file: "{{ lookup('first_found', files) }}"
      vars:
        files:
          - "{{ ansible_facts.distribution }}.yml"
          - default.yml

  always:
    # Reset back to original hostname
    - name: Move back original file if it existed
      become: 'yes'
      command: mv -f {{ _hostname_file }}.orig {{ _hostname_file }}
      when: hn_stat.stat.exists | default(False)

    - name: Delete the file if it never existed
      file:
        path: "{{ _hostname_file }}"
        state: absent
      when: not hn_stat.stat.exists | default(True)

    - name: Reset back to original hostname
      become: 'yes'
      hostname:
        name: "{{ original.stdout }}"
      register: revert

    - name: Ensure original hostname was reset
      assert:
        that:
          - revert is changed