With:
- ansible 3.2.0 (pip3)
- ansible-base 2.10.7 (pip3)
The goal is twofold:
- gather the facts *only from the local cache* when the cache is not empty
and its timeout <fact_caching_timeout> has not expired
- contact the remote network device *only when* the cache is empty or the
timeout has expired
I made some tests with the following settings in /etc/ansible/ansible.cfg:
fact_caching = redis
fact_caching_timeout = 3600
fact_caching_connection = localhost:6379:0:<redis_password>
Making sure that redis is running with *requirepass <redis_password>* in
/etc/redis/redis.conf:
$ sudo systemctl status redis
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service;
enabled; vendor preset: enabled)
Active: active (running) since Fri 2021-04-09 12:09:54 CEST;
7h ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 1610 (redis-server)
Status: "Ready to accept connections"
Tasks: 5 (limit: 18973)
Memory: 5.7M
CGroup: /system.slice/redis-server.service
└─1610 /usr/bin/redis-server 127.0.0.1:6379
Apr 09 12:09:54 host systemd[1]: Starting Advanced key-value
store...
Apr 09 12:09:54 host systemd[1]: Started Advanced key-value store.
Running the following simple play over an IOS device:
- "Gathering Facts" took several seconds indicating that the remote device
has been contacted
- "Pinging remote device to create/update facts cache" was almost
instantaneous
- name: Gathering remote device facts preferably from the cache
hosts: all
vars:
ansible_connection: network_cli
gather_facts: yes
strategy: debug
tasks:
- name: Pinging remote device to create/update facts cache
ping:
leads to:
ansible-playbook 2.10.7
config file = /etc/ansible/ansible.cfg
ansible python module location =
/usr/local/lib/python3.9/dist-packages/ansible
executable location = /usr/local/bin/ansible-playbook
python version = 3.9.4 (default, Apr 4 2021, 19:38:44) [GCC
10.2.1 20210401]
Using /etc/ansible/ansible.cfg as config file
Parsed lab/hosts inventory source with ini plugin
redirecting (type: cache) ansible.builtin.redis to
community.general.redis
Redis connection:
Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
redirecting (type: callback) ansible.builtin.yaml to
community.general.yaml
redirecting (type: callback) ansible.builtin.yaml to
community.general.yaml
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: sdxlive_gather_facts_tests.yml >
------------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
1 plays in sdxlive_gather_facts_tests.yml
__________________________________________________________
/ PLAY [Gathering remote device facts preferably from the \
\ cache] /
----------------------------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
________________________
< TASK [Gathering Facts] >
------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
task path: playbooks/sdxlive_gather_facts_tests.yml:1
redirecting (type: connection) ansible.builtin.network_cli to
ansible.netcommon.network_cli
[WARNING]: Ignoring timeout(30) for cisco.ios.ios_facts
Using module file
/opt/ansible_collections/cisco/ios/plugins/modules/ios_facts.py
Pipelining is enabled.
<172.21.16.79> ESTABLISH LOCAL CONNECTION FOR USER: admin
<172.21.16.79> EXEC /bin/bash -c '/usr/bin/python3 && sleep 0'
ok: [XEv_Spine_31]
META: ran handlers
___________________________________________________________
< TASK [Pinging remote device to create/update facts cache] >
-----------------------------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
task path: playbooks/sdxlive_gather_facts_tests.yml:8
redirecting (type: connection) ansible.builtin.network_cli to
ansible.netcommon.network_cli
Using module file
/usr/local/lib/python3.9/dist-packages/ansible/modules/ping.py
Pipelining is enabled.
<172.21.16.79> ESTABLISH LOCAL CONNECTION FOR USER: admin
<172.21.16.79> EXEC /bin/bash -c '/usr/bin/python3 && sleep 0'
ok: [XEv_Spine_31] => changed=false
invocation:
module_args:
data: pong
ping: pong
META: ran handlers
META: ran handlers
If I use *gather_facts: no *instead, the network facts are not gathered
from the cache at all.
*How can we ensure that the facts are read only from the cache, except when
it is empty for that device or expired?*
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/04a43bb0-af8e-4030-94e1-867bb02a3e04n%40googlegroups.com.