0% found this document useful (0 votes)
5 views2 pages

Variables in Playbooks

The document provides instructions for creating and executing Ansible playbooks using variable files. It outlines the steps to create three YAML files: variable.yaml, setup_variable.yaml, and cli_variable.yaml, each demonstrating different ways to set and print variables. Additionally, it explains how to run these playbooks with command-line arguments to pass variables dynamically.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Variables in Playbooks

The document provides instructions for creating and executing Ansible playbooks using variable files. It outlines the steps to create three YAML files: variable.yaml, setup_variable.yaml, and cli_variable.yaml, each demonstrating different ways to set and print variables. Additionally, it explains how to run these playbooks with command-line arguments to pass variables dynamically.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

L09-10 Exercise 4 - Variables in Playbooks

 Create a file variable.yaml in master terminal window and add the following text to
it.
---
- hosts: all
remote_user: test
tasks:
- name: Set variable 'name'
set_fact:
name: Test machine
- name: Print variable 'name'
debug:
msg: '{{ name }}'

 Let's run it in the usual way:


$ ansible-playbook -i client.test.org, variables.yaml
$ ansible all -i client.test.org, -m setup

 Create a file setup_variable.yaml in master terminal window and add the following
text to it
---
- hosts: all
remote_user: test
tasks:
- name: Print OS and version
debug:
msg: '{{ ansible_distribution }} {{
ansible_distribution_version }}'

 Run it with the following:


$ ansible-playbook -i client.test.org, setup_variables.yaml
 As you can see, it printed the OS name and version, as expected, it's also possible to
pass a variable using a command-line argument

 Create a file cli_variable.yaml in master terminal window and add the following text
to it
---
- hosts: all
remote_user: test
tasks:
- name: Print variable 'name'
debug:
msg: '{{ name }}'

 Execute it with the following:


$ ansible-playbook -i client.test.org, cli_variables.yaml -e
'name=test01'

 In case we forgot to add the additional parameter to specify the variable, we would
have executed it as:
$ ansible-playbook -i client.test.org, cli_variables.yaml

You might also like