Greetings all. I’d like to tap your knowledge to see if there is a way to tweak a playbook to ‘speed things up’. I have a list of apps that always get installed to a new server. If I do them manually, as either a single command:
apt install sendemail libio-socket-ssl-perl libnet-ssleay-perl perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions dos2unix samba ntfs-3g gcc make git-core neofetch net-tools curl chrony linux-headers-$(uname -r) conky python3-pip -y
or individually
apt install app1
apt install app2
…
or as a bash script
it takes 5-7 minutes.
But if I run the install as a vars list using:
Your last apt task … does it need a ‘state: present’ parameter?
- name: install standard stuff
apt:
name: “{{ applications }}”
state: present
Do you support other families of Linux? If so you may want to consider storing your vars in family-specific vars files and including them based on ansible_os_family, and use the package module vs apt/yum modules. Something like this.
vars_files:
- “./common/{{ ansible_os_family }}.yml”
tasks:
- name: install required packages
package:
name: “{{ applications }}”
state: present
- name: update all packages
package:
name: *
state: latest
All these “family” vars files could define the same vars, but each var would have os family specific values such as package names. You use these common vars in your tasks, but the tasks then become OS agnostic because you sourced an OS family specific vars file. In some cases you may need an os distribution and version.
vars_files:
- “./common/{{ (ansible_distribution|lower) + ansible_distribution_major_version }}.yml”
tasks:
- name: install required packages
package:
name: “{{ applications }}”
state: present
- name: update all packages
package:
name: *
state: latest
This would look for files like redhat8.yml, ubuntu22.yml, debian10.yml, amazon2.yml, etc.
I reference a folder called ‘./common’ but you can place them anywhere you like.
Just some thoughts on making playbooks and tasks more future proof …
Walter
Many thanks for rapid responses.
re: need of a ‘state: present’ parameter
- It does not seem to need that line, and even if included, does not make a difference in the amount of time taken.
re: vars files vs vars in playbook
- I have not been able to resolve issues with using any version of a vars.yml file to separate out that information. I currently only support Debian based (Debian / RaspberryPiOS / Ubuntu) Linuxes with ansible. After (If?) I get this working, then I will try expanding to other options. Trying this one step at a time.
vars files look like other yml files.
Spaces are everything, Mr Rowe. Thanks for the hint for my vars.yml issue, (application list in a separate vars.yml file) which is now working as expected. Still takes forever (relatively speaking) to install the apps, but It’s helping me make the actual playbook more readable.
I wish there was a better syntax and space checking tool, ansible’s reminds me of the old Linux printer error message: “it’s on fire”…