FreeKB - ansible-playbook command - set_facts
ansible-playbook command - set_facts

Home > Search


This assumes you have installed Ansible Tower and are familiar with the ansible-playbook command and facts. Variables can be created using vars or set_facts. In this example, set_facts is used to create a variable named "foo" with a value of "Hello World".

---
- hosts: all
  tasks:
    - set_facts: foo="Hello World"

 

debug can be used to print the variable, which will produce output like this.

TASK [debug]
ok: [server1.example.com] => {
    "msg": "Hello World"
}

 

Or, variables can be created on the command line and passed into the playbook, like this.

ansible-playbook playbook.yml -i inventory.yml --extra-vars "foo=Hello bar=World"

 

In this scenario, you would not define the variable in your playbook.yml file, but you would use the variable in your playbook.yml file.

---
- hosts: all
  tasks:
    - set_facts: var1="{{foo}}
    - set_facts: var1="{{bar}}

 


Global variables

By default, variables are isolated to only be accessible within a hosts. Here is now to create a variable that can be used in multiple hosts.

- hosts: all
  tasks:
    - set_fact: foo="Hello World"

- hosts: all
  tasks:
    -debug: msg="{{foo}}"

- hosts: web
  tasks:
    - debug: msg="{{foo}}"

 

Or, you can start like this, if you know the variable only needs to be used in certain hosts.

- hosts: web:db

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter cd728 in the box below so that we can be sure you are a human.




Comments