Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hostname: hostnamectl check -> SystemdStrategy #70532

Merged
merged 3 commits into from Jul 10, 2020

Conversation

relrod
Copy link
Member

@relrod relrod commented Jul 8, 2020

SUMMARY

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

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

hostname

@ansibot ansibot added affects_2.11 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. system System category needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Jul 8, 2020
@relrod relrod force-pushed the modules/hostname/generic-hostnamectl branch from 6a0c67a to 6c69f2f Compare July 8, 2020 23:36
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jul 8, 2020
@relrod relrod force-pushed the modules/hostname/generic-hostnamectl branch from 6c69f2f to e1d8280 Compare July 8, 2020 23:47
@relrod relrod requested a review from samdoran July 8, 2020 23:52
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>
@relrod relrod force-pushed the modules/hostname/generic-hostnamectl branch from e1d8280 to 8ea861d Compare July 9, 2020 03:03
@Akasurde Akasurde self-requested a review July 9, 2020 13:31
Copy link
Contributor

@samdoran samdoran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code changes look good. I think we could refactor the tests a bit, but that's not a blocker to merging this.

lib/ansible/modules/hostname.py Outdated Show resolved Hide resolved
@@ -0,0 +1,79 @@
- block:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block can be moved into the main.yml tasks.

Comment on lines 7 to 15
- include_tasks: rhel.yml
when: ansible_distribution == 'RedHat'

- include_tasks: freebsd.yml
when: ansible_distribution == 'FreeBSD'

# Even check_mode fails in a container. :-(
- import_tasks: check_mode.yml
when: ansible_distribution in ['RedHat', 'FreeBSD']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could put most of the distribution specific tasks in main.yml since they are 90% the same. We just need to add files in vars for each distribution that sets _hostname_file to the correct value.

With conditionals on blocks, I find it's best from a readability perspective to put it at the beginning of the block rather than the end.

Suggested change
- include_tasks: rhel.yml
when: ansible_distribution == 'RedHat'
- include_tasks: freebsd.yml
when: ansible_distribution == 'FreeBSD'
# Even check_mode fails in a container. :-(
- import_tasks: check_mode.yml
when: ansible_distribution in ['RedHat', 'FreeBSD']
# 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', files) }}"
vars:
files:
- "{{ ansible_facts.distribution }}.yml"
- "{{ ansible_facts.os_family }}.yml"
- default.yml
- 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
- name: Get current hostname again
command: hostname
register: after_hn
- assert:
that:
- hn is changed
- original.stdout == after_hn.stdout
- 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: hostname1
- name: Get hostname
command: hostname
register: current_after_hostname1
- name: Run hostname again to ensure it is idempotent
hostname:
name: crocodile.ansible.test.doesthiswork.net.example.com
register: hostname2
- name: Get hostname
command: hostname
register: current_after_hostname2
- assert:
that:
- hostname1 is changed
- hostname2 is not changed
- current_after_hostname1.stdout == 'crocodile.ansible.test.doesthiswork.net.example.com'
- current_after_hostname1.stdout == current_after_hostname2.stdout
- name: Include distribution specific tasks
include_tasks:
file: "{{ lookup('first_found', files) }}"
vars:
files:
- "{{ ansible_facts.distribution }}.yml"
- none.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.stat.exists
- name: Delete the file if it never existed
file:
path: "{{ _hostname_file }}"
state: absent
when: 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

lib/ansible/modules/hostname.py Show resolved Hide resolved
Co-authored-by: Sam Doran <sdoran@redhat.com>
@samdoran samdoran removed the needs_triage Needs a first human triage before being processed. label Jul 9, 2020
Signed-off-by: Rick Elrod <rick@elrod.me>
@relrod relrod merged commit 9fbd659 into ansible:devel Jul 10, 2020
@ansible ansible locked and limited conversation to collaborators Aug 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.11 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team. system System category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants