How to pass variables via command line?

QuestionsQuestions4 SkillsProAnsible Playbook BasicsOct, 15 2025
0204

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:

  1. Passing a Single Variable: You can pass a single variable like this:

    ansible-playbook my-playbook.yml -e "http_port=80"
    
  2. 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"
    
  3. 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}'
    
  4. 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.yml
    
  5. Combining 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.

0 Comments

no data
Be the first to share your comment!