diff options
Diffstat (limited to 'test/integration/targets/hostname/tasks')
8 files changed, 265 insertions, 0 deletions
diff --git a/test/integration/targets/hostname/tasks/Debian.yml b/test/integration/targets/hostname/tasks/Debian.yml new file mode 100644 index 0000000..dfa88fe --- /dev/null +++ b/test/integration/targets/hostname/tasks/Debian.yml @@ -0,0 +1,20 @@ +--- +- name: Test DebianStrategy by setting hostname + become: 'yes' + hostname: + use: debian + name: "{{ ansible_distribution_release }}-bebop.ansible.example.com" + +- name: Test DebianStrategy by getting current hostname + command: hostname + register: get_hostname + +- name: Test DebianStrategy by verifying /etc/hostname content + command: grep -v '^#' /etc/hostname + register: grep_hostname + +- name: Test DebianStrategy using assertions + assert: + that: + - "'{{ ansible_distribution_release }}-bebop.ansible.example.com' in get_hostname.stdout" + - "'{{ ansible_distribution_release }}-bebop.ansible.example.com' in grep_hostname.stdout" diff --git a/test/integration/targets/hostname/tasks/MacOSX.yml b/test/integration/targets/hostname/tasks/MacOSX.yml new file mode 100644 index 0000000..912ced7 --- /dev/null +++ b/test/integration/targets/hostname/tasks/MacOSX.yml @@ -0,0 +1,52 @@ +- name: macOS | Set hostname + hostname: + name: bugs.acme.example.com + +# These tasks can be changed to a loop once https://github.com/ansible/ansible/issues/71031 +# is fixed +- name: macOS | Set hostname specifiying macos strategy + hostname: + name: bugs.acme.example.com + use: macos + +- name: macOS | Set hostname specifiying macosx strategy + hostname: + name: bugs.acme.example.com + use: macosx + +- name: macOS | Set hostname specifiying darwin strategy + hostname: + name: bugs.acme.example.com + use: darwin + +- name: macOS | Get macOS hostname values + command: scutil --get {{ item }} + loop: + - HostName + - ComputerName + - LocalHostName + register: macos_scutil + ignore_errors: yes + +- name: macOS | Ensure all hostname values were set correctly + assert: + that: + - "['bugs.acme.example.com', 'bugs.acme.example.com', 'bugsacmeexamplecom'] == macos_scutil.results | map(attribute='stdout') | list" + +- name: macOS | Set to a hostname using spaces and punctuation + hostname: + name: The Dude's Computer + +- name: macOS | Get macOS hostname values + command: scutil --get {{ item }} + loop: + - HostName + - ComputerName + - LocalHostName + register: macos_scutil_complex + ignore_errors: yes + +- name: macOS | Ensure all hostname values were set correctly + assert: + that: + - "['The Dude\\'s Computer', 'The Dude\\'s Computer', 'The-Dudes-Computer'] == (macos_scutil_complex.results | map(attribute='stdout') | list)" diff --git a/test/integration/targets/hostname/tasks/RedHat.yml b/test/integration/targets/hostname/tasks/RedHat.yml new file mode 100644 index 0000000..1f61390 --- /dev/null +++ b/test/integration/targets/hostname/tasks/RedHat.yml @@ -0,0 +1,15 @@ +- name: Make sure we used SystemdStrategy... + lineinfile: + path: "{{ _hostname_file }}" + line: crocodile.ansible.test.doesthiswork.net.example.com + check_mode: true + register: etc_hostname + failed_when: etc_hostname is changed + +- name: ...and not RedhatStrategy + lineinfile: + path: /etc/sysconfig/network + line: HOSTNAME=crocodile.ansible.test.doesthiswork.net.example.com + check_mode: true + register: etc_sysconfig_network + failed_when: etc_sysconfig_network is not changed diff --git a/test/integration/targets/hostname/tasks/check_mode.yml b/test/integration/targets/hostname/tasks/check_mode.yml new file mode 100644 index 0000000..e25df97 --- /dev/null +++ b/test/integration/targets/hostname/tasks/check_mode.yml @@ -0,0 +1,20 @@ +# These are less useful (check_mode only) but run even in containers +- block: + - name: Get current hostname + command: hostname + register: original + + - name: Change hostname (check_mode) + hostname: + name: crocodile.ansible.test.doesthiswork.net.example.com + check_mode: true + register: hn + + - name: Get current hostname again + command: hostname + register: after_hn + + - assert: + that: + - hn is changed + - original.stdout == after_hn.stdout diff --git a/test/integration/targets/hostname/tasks/default.yml b/test/integration/targets/hostname/tasks/default.yml new file mode 100644 index 0000000..b308239 --- /dev/null +++ b/test/integration/targets/hostname/tasks/default.yml @@ -0,0 +1,2 @@ +- debug: + msg: No distro-specific tests defined for this distro. diff --git a/test/integration/targets/hostname/tasks/main.yml b/test/integration/targets/hostname/tasks/main.yml new file mode 100644 index 0000000..596dd89 --- /dev/null +++ b/test/integration/targets/hostname/tasks/main.yml @@ -0,0 +1,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 diff --git a/test/integration/targets/hostname/tasks/test_check_mode.yml b/test/integration/targets/hostname/tasks/test_check_mode.yml new file mode 100644 index 0000000..9ba1d65 --- /dev/null +++ b/test/integration/targets/hostname/tasks/test_check_mode.yml @@ -0,0 +1,50 @@ +- name: Run hostname module in check_mode + hostname: + name: crocodile.ansible.test.doesthiswork.net.example.com + check_mode: true + register: hn1 + +- name: Get current hostname again + command: hostname + register: after_hn + +- name: Ensure hostname changed properly + assert: + that: + - hn1 is changed + - original.stdout == after_hn.stdout + +- when: _hostname_file is defined and _hostname_file + block: + - name: See if current hostname file exists + stat: + path: "{{ _hostname_file }}" + register: hn_stat + + - name: Move the current hostname file if it exists + command: mv {{ _hostname_file }} {{ _hostname_file }}.orig + when: hn_stat.stat.exists + + - name: Run hostname module in check_mode + hostname: + name: crocodile.ansible.test.doesthiswork.net.example.com + check_mode: true + register: hn + + - stat: + path: /etc/rc.conf.d/hostname + register: hn_stat_checkmode + + - assert: + that: + # TODO: This is a legitimate bug and will be fixed in another PR. + # - not hn_stat_checkmode.stat.exists + - hn is changed + + - name: Get hostname again + command: hostname + register: current_after_cm + + - assert: + that: + - original.stdout == current_after_cm.stdout diff --git a/test/integration/targets/hostname/tasks/test_normal.yml b/test/integration/targets/hostname/tasks/test_normal.yml new file mode 100644 index 0000000..9534d73 --- /dev/null +++ b/test/integration/targets/hostname/tasks/test_normal.yml @@ -0,0 +1,54 @@ +- name: Ensure hostname doesn't confuse NetworkManager + when: ansible_os_family == 'RedHat' and ansible_distribution_major_version is version('8') + block: + - name: slurp /var/log/messages + slurp: + path: /var/log/messages + become: yes + register: messages_before + + - assert: + that: + - > + 'current hostname was changed outside NetworkManager' not in messages_before.content|b64decode + +- name: Run hostname module for real now + become: 'yes' + hostname: + name: crocodile.ansible.test.doesthiswork.net.example.com + register: hn2 + +- name: Get hostname + command: hostname + register: current_after_hn2 + +- name: Ensure hostname doesn't confuse NetworkManager + when: ansible_os_family == 'RedHat' and ansible_distribution_major_version is version('8') + block: + - name: slurp /var/log/messages + slurp: + path: /var/log/messages + become: yes + register: messages_after + + - assert: + that: + - > + 'current hostname was changed outside NetworkManager' not in messages_after.content|b64decode + +- name: Run hostname again to ensure it does not change + become: 'yes' + hostname: + name: crocodile.ansible.test.doesthiswork.net.example.com + register: hn3 + +- name: Get hostname + command: hostname + register: current_after_hn3 + +- assert: + that: + - hn2 is changed + - hn3 is not changed + - current_after_hn2.stdout == 'crocodile.ansible.test.doesthiswork.net.example.com' + - current_after_hn2.stdout == current_after_hn2.stdout |