mirror of https://github.com/ansible/ansible.git
hostname: hostnamectl check -> SystemdStrategy (#70532)
Change: - Move hostnamectl check out of GenericStrategy because it was incorrect for everything except the SystemdStrategy which is where it belongs. - Add some initial tests for the hostname module, though we are limited by the fact that we can't do much testing with it in containers. Test Plan: - new hostname integration tests Signed-off-by: Rick Elrod <rick@elrod.me> Co-authored-by: Sam Doran <sdoran@redhat.com>pull/70548/head
parent
b149a89e07
commit
9fbd65958d
@ -0,0 +1,4 @@
|
|||||||
|
shippable/posix/group1
|
||||||
|
destructive
|
||||||
|
skip/aix # currently unsupported by hostname module
|
||||||
|
skip/osx # same, see #54439, #32214
|
@ -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
|
@ -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
|
@ -0,0 +1,2 @@
|
|||||||
|
- debug:
|
||||||
|
msg: No distro-specific tests defined for this distro.
|
@ -0,0 +1,121 @@
|
|||||||
|
# Setting the hostname in our test containers doesn't work currently
|
||||||
|
- when: ansible_facts.virtualization_type != 'docker'
|
||||||
|
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
|
||||||
|
|
||||||
|
- 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
|
||||||
|
|
||||||
|
- 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
|
||||||
|
|
||||||
|
- name: Run hostname module for real now
|
||||||
|
hostname:
|
||||||
|
name: crocodile.ansible.test.doesthiswork.net.example.com
|
||||||
|
register: hn2
|
||||||
|
|
||||||
|
- name: Get hostname
|
||||||
|
command: hostname
|
||||||
|
register: current_after_hn2
|
||||||
|
|
||||||
|
- name: Run hostname again to ensure it is idempotent
|
||||||
|
hostname:
|
||||||
|
name: crocodile.ansible.test.doesthiswork.net.example.com
|
||||||
|
register: hnidem
|
||||||
|
|
||||||
|
- name: Get hostname
|
||||||
|
command: hostname
|
||||||
|
register: current_after_hnidem
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- hn2 is changed
|
||||||
|
- hnidem is not changed
|
||||||
|
- current_after_hn2.stdout == 'crocodile.ansible.test.doesthiswork.net.example.com'
|
||||||
|
- current_after_hn2.stdout == current_after_hnidem.stdout
|
||||||
|
|
||||||
|
- 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
|
||||||
|
command: mv -f {{ _hostname_file }}.orig {{ _hostname_file }}
|
||||||
|
when: hn_stat is defined and hn_stat.stat.exists
|
||||||
|
|
||||||
|
- name: Delete the file if it never existed
|
||||||
|
file:
|
||||||
|
path: "{{ _hostname_file }}"
|
||||||
|
state: absent
|
||||||
|
when: hn_stat is defined and not hn_stat.stat.exists
|
||||||
|
|
||||||
|
# Reset back to original hostname
|
||||||
|
- hostname:
|
||||||
|
name: "{{ original.stdout }}"
|
||||||
|
register: revert
|
||||||
|
|
||||||
|
# And make sure we really do
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- revert is changed
|
@ -0,0 +1 @@
|
|||||||
|
_hostname_file: /etc/rc.conf.d/hostname
|
@ -0,0 +1 @@
|
|||||||
|
_hostname_file: /etc/hostname
|
@ -0,0 +1 @@
|
|||||||
|
_hostname_file: ~
|
Loading…
Reference in New Issue