Selectively running single role in Ansible

· 103 words · 1 minute read

While I’ve been switching from Ansible to Nix lately, I still use Ansible and have written a fair share of Ansible YAML. A common trick I’ve used is to only run a specific Ansible role instead of the whole playbook. That can be achieved by using tags, but usually I haven’t tagged everything before I have the need.

I have this short playbook stored in the project root next to the roles/ directory:

# vim: set ft=yaml:
# usage: ansible-playbook -l test -e role=debian runrole.yaml
- hosts: all
  remote_user: "{{remote_user|default('root')}}"
  - '{{role}}'

Does what it promises and also runs role dependencies.