Index ¦ Archives ¦ Atom

One-off ansible bootstrapping playbooks

The case for simple playbooks

For my private infrastructure it doesn't make sense to create full-blown ansible roles unless I'm showing off. Therefore I use a set of simple, ansible playbooks that do one thing and cover the most common tasks. These playbooks are mostly intended to make the new server environment more hospitable. For example, on Fedora, I use this playbook to install packages:

---
- hosts: all
  remote_user: root
  tasks:
    - name: install basic packages
      dnf: name="{{item}}" state=installed
      with_items:
        - tmux
        - vim
        - bind-utils
        - nmap
        - policycoreutils-python-utils
        - cronie

You might have noticed that in the above script, the dangerous wildcard "all" is used which would under normal circumstances apply to all hosts available in the default inventory file. But, we can override the inventory used by ansible-playbook via the -i flag.

The hack

Usually, one would have to edit the above hosts variable to run the playbook. However, if we override the inventory file like this:

ansible-playbook -i "my.example.com," install-packages.yaml

Note the comma at the end of my.example.com. Without the comma, the above command will error out.

If this is the first time testing the above inventory option, use the ping module (to avoid running a command on the entire content of your usual inventory file):

ansible my.example.com -i "my.example.com," -u aoiop -m ping

Credit

I originally found the above hack on StackOverflow while trying to avoid unnecessary playbook edits which don't mesh very well with version control.

© Bruno Henc. Built using Pelican. Theme by Giulio Fidente on github.