Hi everyone, think by now most of us are settling into long-term working from home routine as part of COVID-19 social distancing. Hope you and your familiy are healthy and safe.
Ansible Reference – am building the ultimate go-to page for all things Ansible
Skip Gathering Facts in Ansible
There are Ansible playbooks which depend on the most up-to-date information found on each node. That’s where fact gathering is a much needed help. But there are also simpler more predefined playbooks, which don’t need fact gathering and can therefore gain performance if no facts are collected.
Why Fact Gathering in Ansible Takes Time
Fact gathering means Ansible runs a number of commands to confirm the most recent values for important indicators and parameters.
Run against my freshly installed RHEL 8 based PC, this takes roughly 4 seconds. Part of this can be to how RHEL is configured (and that it’s still a work in progress), but part of this amount of time is defined by the sheer number of facts: more than 1000!
Typical Facts Collected By Ansible
This is not a complete list, I’m just giving you examples to indicate why facts collection may be time consuming:
filesystems and logical volume managers (objects, types, sizes)
OS distro information
network devices and full list of their capabilities
environment variables
Disable Fact Gathering in Ansible
Since I don’t really need to re-establish hardware specs or logical volumes layout of my RHEL 8 desktop every time I run some Ansible post-configuration, I decided to disable fact gathering and shave 4-5 sec at the start of each playbook run.
Simply specify this at the top of your Ansible playbook:
gather_facts: no
In on of my playbooks, this is how it looks:
---
- name: Baseline
hosts: desktops
gather_facts: no
This really made a noticeable difference. Have fun!
Turns out, become_user directive can be used not only for privilege escalation (running Ansible playbooks as root), but also for becoming any other when you want certain tasks run as that user instead of root.
Default Ansible Behavior for Running Tasks
I had the following piece of code, running /home/greys/.dotfiles/install script. It didn’t run as intended, creating symlinks in /root directory (because that’s what Ansible was running the task as):
become_user parameter can be specifed per task or per playbook, apparently. So that’s how you specify it per task – in my example to run the Create symlinks for dotfiles task as my user greys: