Ansible supports the use of variables to better customize tasks and PlayBook execution. This way, you can use the same PlayBook for different purposes and environments.

Variables can come from different sources, such as the game manual file itself or an external variable file imported into the game manual. Special precedence rules apply when dealing with multiple variable sources that define variables with the same name.

To see how variables work in practice, we will create a new test game manual that will print the values of both variables. Username and home_dir. Create a new file named playbook-02.yml in your ansible-practice directory.

nano ~/ansible-practice/playbook-02.yml
Copy the code

Then add the following lines to the new PlayBook file.

~/ansible-practice/playbook-02.yml

---
- hosts: all
  vars:
    - username: sammy
    - home: /home/sammy   
  tasks:
    - name: print variables
      debug:
        msg: "Username: {{ username }}, Home dir: {{ home }}"
Copy the code

Save and close the file after editing.

The Vars section of the Playbook defines a list of variables that will be injected into the play scope. All missions and any files or templates that may be included in the game manual have access to these variables.

To try out this game rule on a server in your manifest file, run ansible-Playbook with the same connection parameters you used earlier when running the first example. Again, we will use an inventory file named Inventory and Sammy user to connect to the remote server.

ansible-playbook -i inventory playbook-02.yml -u sammy
Copy the code

You’ll see something like this output.

Output PLAY [all] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *********************************************************** TASK [Gathering Facts] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *********************************************** ok: [203.0.113.10] TASK [print variables] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ok: [203.0.113.10] = > {" MSG ":" Username: Sammy, Home dir: /home/sammy" } PLAY RECAP * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 203.0.113.10: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Copy the code

The Print Variables task will use the Debug module to print the values of the two variables we defined in the Vars section of the Playbook.