If you ever wanted pass in ‘parameters’ to your Ansible playbooks via the command line when you are running the playbook through ansible-playbook here is the way to do it.
Command line
ansible-playbook --extra-vars '{"NODE_ENV":"staging","GIT_BRANCH":"develop"}' playbook.yml
or the shorter version (thanks mpdehaan)
ansible-playbook "-e 'NODE_ENV=staging GIT_BRANCH=develop'" playbook.yml
Here you are setting NODE_ENV and GIT_BRANCH variables (if it wasn’t obvious).
Now inside your playbook you will have something like the following
- name: Node apps deploy
hosts: all
gather_facts: no
vars:
git_branch: "{{ GIT_BRANCH|default('develop') }}"
node_env: "{{ NODE_ENV|default('staging') }}"
tasks:
- command echo "{{ node_env }}"
- command echo "{{ git_branch }}"
That’s about it.
Update: Use a shorter version of the ansible-playbook command when passing in extra-vars. As pointed out by mpdehaan (thanks!!).