Bootstrapping Ansible via Ansible
Ansible is a simple automation tool that is agentless, meaning all it requires to run on a node is SSH and Python.
So what if we want to set up a new machine to be able to be managed via Ansible, but for whatever reason Python isn’t installed on that machine? Sure, we could log in to the box and install it manually, but it is also possible to use Ansible to bootstrap Ansible even if Python doesn’t exist on it.
Enter: the raw module!
This method only requires root SSH. Ansible can connect to the machine as root and execute the following task:
- name: Install Python without using Ansible modules
raw: bash -c "test -e /usr/bin/python3 || yum install -y python3"
The above task tests if /usr/bin/python3 already exists and if not, installs it via YUM. Pretty straight forward, but what if we want to bootstrap servers from multiple potential families?
This can be extended with additional tests to also support Debian/Ubuntu:
- name: Install Python without using Ansible modules
raw: >
bash -c "test -e /usr/bin/python3 ||
(test -e /usr/bin/yum && yum install -y python3) ||
(apt install -y python3 python-apt)
This can be extended further as needed into potential XKCD territory.