ansible-collections / cisco.ios

Ansible Network Collection for Cisco IOS
GNU General Public License v3.0
262 stars 161 forks source link

cisco.ios.ios_facts fact caching issue #1003

Closed Andyjb8 closed 4 months ago

Andyjb8 commented 5 months ago
SUMMARY

Fact caching does not seem to be working with cisco.ios.ios_facts

ISSUE TYPE
COMPONENT NAME

cisco.ios.ios_facts

ANSIBLE VERSION
ansible [core 2.15.8]
  config file = /home/andrewba/ansible.cfg
  configured module search path = ['/home/andrewba/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/andrewba/automation/collections/ansible_collections
  executable location = /usr/bin/ansible
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
COLLECTION VERSION
# /home/user/automation/collections/ansible_collections
Collection Version
---------- -------
cisco.ios  6.0.0  
CONFIGURATION
ansible-config dump --only-changed
CACHE_PLUGIN(/home/user/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/home/user/ansible.cfg) = /var/tmp/facts/
CACHE_PLUGIN_PREFIX(/home/user/ansible.cfg) = ansible_facts
CACHE_PLUGIN_TIMEOUT(/home/user/ansible.cfg) = 86400
CALLBACKS_ENABLED(/home/user/ansible.cfg) = ['profile_tasks']
COLLECTIONS_PATHS(/home/user/ansible.cfg) = ['/home/user/automation/collections/ansible_collections']
COLOR_DEBUG(/home/user/ansible.cfg) = dark gray
COLOR_ERROR(/home/user/ansible.cfg) = yellow
COLOR_VERBOSE(/home/user/ansible.cfg) = cyan
CONFIG_FILE() = /home/user/ansible.cfg
DEFAULT_GATHERING(/home/user/ansible.cfg) = explicit
DEFAULT_HOST_LIST(/home/user/ansible.cfg) = ['/home/user/automation/inventory/hosts.ini']
DEFAULT_LOG_PATH(/home/user/ansible.cfg) = /home/user/ansible.log
DEFAULT_VAULT_PASSWORD_FILE(/home/user/ansible.cfg) = /home/user/vault.pass
HOST_KEY_CHECKING(/home/user/ansible.cfg) = False
INVENTORY_CACHE_ENABLED(/home/user/ansible.cfg) = False
INVENTORY_ENABLED(/home/user/ansible.cfg) = ['ini', 'yaml', 'auto', 'host_list', 'script', 'solarwinds.orion.ori>
PERSISTENT_COMMAND_TIMEOUT(/home/user/ansible.cfg) = 120
PERSISTENT_CONNECT_TIMEOUT(/home/user/ansible.cfg) = 60
RETRY_FILES_ENABLED(/home/user/ansible.cfg) = True
RETRY_FILES_SAVE_PATH(/home/user/ansible.cfg) = /home/user/retry
OS / ENVIRONMENT

Description: Ubuntu 22.04.3 LTS Release: 22.04 Codename: jammy

STEPS TO REPRODUCE

ansible-playbook test.yml --limit RT-01 -vvvvv

#  test.yml playbook to get interface facts from cisco nodes.

- name: Get Interface Facts
  hosts: all
  gather_facts: false

  tasks:

    - name: Gather IOS facts
      cisco.ios.ios_facts:
        gather_subset: interfaces
EXPECTED RESULTS

I expect it to retrieve facts from cache on subsequent runs, It is creating the ansible facts file as shown below, but it updates the fact file timestamp every time the playbook is run and it doesn't appear to run any faster on subsequent runs. I would think the debug would show the module retrieving facts from cache as well, but I am not seeing anything indicating that it is. It is taking about 33 seconds to gather facts in my lab and I feel it should be faster if using cached facts.

user@al-svr-ansb-01:~$ ls -al /var/tmp/facts/ansible_factsRT-01 -rw------- 1 user user 6829 Dec 28 15:51 /var/tmp/facts/ansible_factsRT-01

ACTUAL RESULTS
TASK [Gather IOS facts] *********************************************************************************************
task path: /home/user/automation/playbooks/solarwinds/orion_add_interfaces_to_monitor.yml:33
Thursday 28 December 2023  16:20:02 +0000 (0:00:01.460)       0:00:01.549 ***** 
Found a vault_id (default) in the vaulttext
We have a secret associated with vault id (default), will try to use to decrypt None
Trying to use vault secret=(FileVaultSecret(filename='/home/user/vault.pass')) id=default to decrypt None
Trying secret FileVaultSecret(filename='/home/user/vault.pass') for vault_id=default
Decrypt successful with secret=FileVaultSecret(filename='/home/user/vault.pass') and vault_id=default
Loading collection ansible.utils from /home/user/automation/collections/ansible_collections/ansible/utils
redirecting (type: terminal) ansible.builtin.ios to cisco.ios.ios
redirecting (type: cliconf) ansible.builtin.ios to cisco.ios.ios
Found a vault_id (default) in the vaulttext
We have a secret associated with vault id (default), will try to use to decrypt None
Trying to use vault secret=(FileVaultSecret(filename='/home/user/vault.pass')) id=default to decrypt None
Trying secret FileVaultSecret(filename='/home/user/vault.pass') for vault_id=default
Decrypt successful with secret=FileVaultSecret(filename='/home/user/vault.pass') and vault_id=default
Found a vault_id (default) in the vaulttext
We have a secret associated with vault id (default), will try to use to decrypt None
Trying to use vault secret=(FileVaultSecret(filename='/home/user/vault.pass')) id=default to decrypt None
Trying secret FileVaultSecret(filename='/home/user/vault.pass') for vault_id=default
Decrypt successful with secret=FileVaultSecret(filename='/home/user/vault.pass') and vault_id=default
<x.x.0.1> attempting to start connection
<x.x.0.1> using connection plugin ansible.netcommon.network_cli
Found ansible-connection at path /usr/bin/ansible-connection
<x.x.0.1> found existing local domain socket, using it!
<x.x.0.1> updating play_context for connection
<x.x.0.1> Loading collection ansible.builtin from 
<x.x.0.1> local domain socket path is /home/user/.ansible/pc/b875a6b1b8
redirecting (type: action) cisco.ios.ios_facts to cisco.ios.ios
redirecting (type: action) cisco.ios.ios_facts to cisco.ios.ios
<x.x.0.1> ANSIBLE_NETWORK_IMPORT_MODULES: enabled
<x.x.0.1> ANSIBLE_NETWORK_IMPORT_MODULES: found cisco.ios.ios_facts  at /home/user/automation/collections/ansible_collections/cisco/ios/plugins/modules/ios_facts.py
<x.x.0.1> ANSIBLE_NETWORK_IMPORT_MODULES: running cisco.ios.ios_facts
<x.x.0.1> ANSIBLE_NETWORK_IMPORT_MODULES: complete
<x.x.0.1> ANSIBLE_NETWORK_IMPORT_MODULES: Result:

 <---- fact output omitted---- >

Gather IOS facts -------------------------------------------------------------------------------------------- 33.81s
roverflow commented 4 months ago

Hey @Andyjb8, Retrieving facts from cache is not possible as we do not support caching of facts in collection level. If you have any further questions feel free to reach out!

Andyjb8 commented 4 months ago

Hmm, but it is caching the facts as it's creating the fact file for my test router in "/var/tmp/facts/" which is the path defined in my ansible.cfg. See below showing it is caching the facts. So I guess the module isn't able to retrieve the cached facts? Is this something that could be added in the future?

fact caching settings in ansible.cfg

fact_caching = jsonfile
fact_caching_connection= /var/tmp/facts/
fact_caching_prefix=ansible_facts
fact_caching_timeout=86400

Here is my playbook: gather_facts.yml

- name: Get Facts
  hosts: all
  gather_facts: False

  tasks:
      - name: Gather IOS facts
        cisco.ios.ios_facts:
          gather_subset: interfaces

Running the playbook:

admin@ansible:~$  ansible-playbook playbooks/gather_fact.yml --limit RT-02 -vvv
ansible-playbook [core 2.15.8]
  config file = /home/admin/ansible.cfg
  configured module search path = ['/home/admin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/admin/automation/collections/ansible_collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
Using /home/admin/ansible.cfg as config file
ERROR! the playbook: playbooks/gather_fact.yml could not be found
admin@ansible:~$  ansible-playbook automation/playbooks/gather_fact.yml --limit RT-02 -vvv
ansible-playbook [core 2.15.8]
  config file = /home/admin/ansible.cfg
  configured module search path = ['/home/admin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/admin/automation/collections/ansible_collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
Using /home/admin/ansible.cfg as config file
Parsed /home/admin/automation/inventory/hosts.ini inventory source with ini plugin
redirecting (type: action) cisco.ios.ios_facts to cisco.ios.ios
redirecting (type: callback) ansible.builtin.profile_tasks to ansible.posix.profile_tasks
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: gather_fact.yml ********************************************************************************
1 plays in automation/playbooks/gather_fact.yml

PLAY [Get Facts] *****************************************************************************************
redirecting (type: action) cisco.ios.ios_facts to cisco.ios.ios

TASK [Gather IOS facts] **********************************************************************************
task path: /home/admin/automation/playbooks/gather_fact.yml:6
Monday 08 January 2024  16:51:54 +0000 (0:00:00.083)       0:00:00.083 ******** 
redirecting (type: terminal) ansible.builtin.ios to cisco.ios.ios
redirecting (type: cliconf) ansible.builtin.ios to cisco.ios.ios
redirecting (type: action) cisco.ios.ios_facts to cisco.ios.ios
redirecting (type: action) cisco.ios.ios_facts to cisco.ios.ios
...
<fact debug output ommitted>
....
PLAY RECAP ***********************************************************************************************
RT-02                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Monday 08 January 2024  16:52:41 +0000 (0:00:46.487)       0:00:46.570 ******** 
=============================================================================== 
Gather IOS facts --------------------------------------------------------------------------------- 46.49s

And it created the fact file:

admin@ansible:~$ ls -al /var/tmp/facts/ansible_factsRT-02
-rw------- 1 admin admin 7163 Jan  8 16:45 /var/tmp/facts/ansible_factsRT-02

admin@ansible:~$ more /var/tmp/facts/ansible_factsRT-02
{
    "ansible_net_all_ipv4_addresses": [
        "x.x.0.9",
        "x.x.2.10",
        "x.x.2.4",
        "x.x.2.6",
        "x.x.1.2",
        "x.x.1.6",
        "x.x.0.2",
        "x.x.1.20",
        "x.x.1.22"
    ],
    "ansible_net_all_ipv6_addresses": [],
    "ansible_net_api": "cliconf",
    "ansible_net_gather_network_resources": [],
    "ansible_net_gather_subset": [
        "default",
        "interfaces"
    ],
    "ansible_net_hostname": "RT-02",
    "ansible_net_image": "bootflash:packages.conf",
    "ansible_net_interfaces": {
        "GigabitEthernet1": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": "Full",
            "ipv4": [
                {
                    "address": "x.x.0.9",
                    "subnet": "31"
                }
            ],
            "lineprotocol": "up",
            "macaddress": "5254.000d.941c",
            "mediatype": "Virtual",
            "mtu": 1500,
            "operstatus": "up",
            "type": "CSR vNIC"
        },
        "GigabitEthernet2": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": "Full",
            "ipv4": [],
            "lineprotocol": "up",
            "macaddress": "5254.0005.3631",
            "mediatype": "Virtual",
            "mtu": 1500,
            "operstatus": "up",
            "type": "CSR vNIC"
        },
        "GigabitEthernet2.20": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": null,
            "ipv4": [
                {
                    "address": "x.x.2.10",
                    "subnet": "31"
                }
            ],
            "lineprotocol": "up",
            "macaddress": "5254.0005.3631",
            "mediatype": null,
            "mtu": 1500,
            "operstatus": "up",
            "type": "CSR vNIC"
        },
        "GigabitEthernet3": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": "Full",
            "ipv4": [],
            "lineprotocol": "up",
            "macaddress": "5254.0015.aeff",
            "mediatype": "Virtual",
            "mtu": 1500,
            "operstatus": "up",
            "type": "CSR vNIC"
        },
        "GigabitEthernet3.200": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": null,
            "ipv4": [
                {
                    "address": "x.x.2.4",
                    "subnet": "31"
                }
            ],
            "lineprotocol": "up",
            "macaddress": "5254.0015.aeff",
            "mediatype": null,
            "mtu": 1500,
            "operstatus": "up",
            "type": "CSR vNIC"
        },
        "GigabitEthernet3.201": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": null,
            "ipv4": [
                {
                    "address": "x.x.2.6",
                    "subnet": "31"
                }
            ],
            "lineprotocol": "up",
            "macaddress": "5254.0015.aeff",
            "mediatype": null,
            "mtu": 1500,
            "operstatus": "up",
            "type": "CSR vNIC"
        },
        "GigabitEthernet4": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": "Full",
            "ipv4": [
                {
                    "address": "x.x.1.2",
                    "subnet": "31"
                }
            ],
            "lineprotocol": "up",
            "macaddress": "5254.0014.8d73",
            "mediatype": "Virtual",
            "mtu": 1500,
            "operstatus": "up",
            "type": "CSR vNIC"
        },
        "GigabitEthernet5": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": "Full",
            "ipv4": [
                {
                    "address": "x.x.1.6",
                    "subnet": "31"
                }
            ],
            "lineprotocol": "up",
            "macaddress": "5254.0005.4947",
            "mediatype": "Virtual",
            "mtu": 1500,
            "operstatus": "up",
            "type": "CSR vNIC"
        },
        "GigabitEthernet6": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": "Full",
            "ipv4": [],
            "lineprotocol": "down",
            "macaddress": "5254.0005.69b9",
            "mediatype": "Virtual",
            "mtu": 1500,
            "operstatus": "administratively down",
            "type": "CSR vNIC"
        },
        "GigabitEthernet7": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": "Full",
            "ipv4": [],
            "lineprotocol": "down",
            "macaddress": "5254.0001.4e47",
            "mediatype": "Virtual",
            "mtu": 1500,
            "operstatus": "administratively down",
            "type": "CSR vNIC"
        },
        "GigabitEthernet8": {
            "bandwidth": 1000000,
            "description": null,
            "duplex": "Full",
            "ipv4": [],
            "lineprotocol": "down",
            "macaddress": "5254.0001.4c0c",
            "mediatype": "Virtual",
            "mtu": 1500,
            "operstatus": "administratively down",
            "type": "CSR vNIC"
        },
        "Loopback0": {
            "bandwidth": 8000000,
            "description": null,
            "duplex": null,
            "ipv4": [
                {
                    "address": "x.x.0.2",
                    "subnet": "32"
                }
            ],
            "lineprotocol": "up",
            "macaddress": null,
            "mediatype": null,
            "mtu": 1514,
            "operstatus": "up",
            "type": null
        },
        "Tunnel20": {
            "bandwidth": 100,
            "description": "to -02",
            "duplex": null,
            "ipv4": [
                {
                    "address": "x.x.1.20",
                    "subnet": "31"
                }
            ],
            "lineprotocol": "up",
            "macaddress": null,
            "mediatype": null,
            "mtu": 9946,
            "operstatus": "up",
            "type": null
        },
        "Tunnel21": {
            "bandwidth": 100,
            "description": "to -01",
            "duplex": null,
            "ipv4": [
                {
                    "address": "x.x.1.22",
                    "subnet": "31"
                }
            ],
            "lineprotocol": "up",
            "macaddress": null,
            "mediatype": null,
            "mtu": 9946,
            "operstatus": "up",
            "type": null
        }
    },
    "ansible_net_iostype": "IOS-XE",
    "ansible_net_model": "CSR1000V",
    "ansible_net_neighbors": {},
    "ansible_net_operatingmode": "autonomous",
    "ansible_net_python_version": "3.10.12",
    "ansible_net_serialnum": "97YCT7LQSZG",
    "ansible_net_system": "ios",
    "ansible_net_version": "17.03.04a",
    "ansible_network_resources": {}
}
roverflow commented 4 months ago

@Andyjb8, while the module does create a cache file, it doesn't actively utilize the cache based on my current understanding. This isn't a priority for us at the moment, but it's certainly something we plan to explore in the future. Hope this helps. Thanks