'Prevent re-use of cached facts in Ansible play

I'm trying to get an Ansible playbook to change the hostname of a Cisco device as described in the basic getting started guide here (step #3). The hostname gets successfully changed on the actual Cisco device but Ansible still outputs the old hostname in the last task, unlike what is displayed in the getting started guide link (new hostname should be displayed).

I've been Googling/experiemnting and suspect something about fact caching, but have not found an answer for the discrepancy between the Ansible guide and my result. Can anyone spot if I'm doing something obviously wrong?

My playbook:

    ---

- name: Network Getting Started First Playbook Extended
  connection: ansible.netcommon.network_cli
  gather_facts: false
  hosts: all
  tasks:

    - name: Get config for ios devices
      cisco.ios.ios_facts:
        gather_subset: all

    - name: Display the config
      debug:
        msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"

    - name: Update the hostname
      become: yes
      become_method: enable
      cisco.ios.ios_config:
        backup: yes
        lines:
          - hostname newname

    - name: Get changed config for ios devices
      cisco.ios.ios_facts:
        gather_subset: all

    - name: Display the changed config
      debug:
        msg: "The new hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"

My output:

 ansible-playbook -i 192.168.1.73, -u admin -k -e ansible_network_os=ios first_playbook_ext_ios.yml --ask-become-pass
SSH password:
BECOME password[defaults to SSH password]:

PLAY [Network Getting Started First Playbook Extended] **************************************************************************************************************************************

TASK [Get config for ios devices] ***********************************************************************************************************************************************************
ok: [192.168.1.73]

TASK [Display the config] *******************************************************************************************************************************************************************
ok: [192.168.1.73] => {
    "msg": "The hostname is oldname and the OS is 17.03.04a"
}

TASK [Update the hostname] ******************************************************************************************************************************************************************
[WARNING]: To ensure idempotency and correct diff the input configuration lines should be similar to how they appear if present in the running configuration on device
changed: [192.168.1.73]

TASK [Get changed config for ios devices] ***************************************************************************************************************************************************
ok: [192.168.1.73]

TASK [Display the changed config] ***********************************************************************************************************************************************************
ok: [192.168.1.73] => {
    "msg": "The new hostname is oldname and the OS is 17.03.04a"
}

PLAY RECAP **********************************************************************************************************************************************************************************
192.168.1.73               : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

As you can see after gathering facts about the device after the hostname change, Ansible still outputs the old hostname "oldname". To confirm, the hostname did actually change on the device and a subsequent run of the same playbook does output the new hostname. Console output from the Cisco device in question is as expected:

oldname#
oldname#
oldname#
*Feb 25 17:46:47.635: %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success [user: admin] [Source: 192.168.1.87] [localport: 22] at 17:46:47 UTC Fri Feb 25 2022
*Feb 25 17:46:51.692: %SYS-5-CONFIG_I: Configured from console by admin on vty0 (192.168.1.87)
*Feb 25 17:46:55.033: %SYS-6-LOGOUT: User admin has exited tty session 1(192.168.1.87)
newname#
newname#
newname#

I had the same results with a Cisco NXOS device as with this IOS device (hostname gets change by Ansible still returns old hostname).

My versions is: ansible [core 2.12.2], python version = 3.8.0, PRETTY_NAME="Ubuntu 18.04.6 LTS"

I've also tried setting 'gathering=explicit' in ansible.cfg but no change in behavior.

Why advice?

Thank you in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source