You can pass variables to an Ansible playbook via the command line using the -e or --extra-vars option. This allows you to define variables at runtime. Here’s how to do it:
Passing a Single Variable: You can pass a single variable like this:
ansible-playbook my-playbook.yml -e "http_port=80"Passing Multiple Variables: You can pass multiple variables by separating them with spaces:
ansible-playbook my-playbook.yml -e "http_port=80 db_port=3306"Passing Variables as a JSON String: You can also pass variables in JSON format:
ansible-playbook my-playbook.yml -e '{"http_port": 80, "db_port": 3306}'Using a Variable File: If you have a file containing variables, you can include it using the
@symbol:ansible-playbook my-playbook.yml -e @vars.ymlCombining Methods: You can combine command line variables with variable files:
ansible-playbook my-playbook.yml -e @vars.yml -e "http_port=80"
Using these methods, you can dynamically set variables for your Ansible playbooks when executing them from the command line.
