Inscreva-se no feed

When you write an Ansible playbook, you sometimes need to pass data into your play at runtime. To do that, you can use a variable, a sort of placeholder for data that's meant to be determined at some point in the future.

There are lots of places to create variables for your playbooks, such as an inventory file, included files, or even dynamically in your playbook itself. However, you can also pass variables in the terminal when you launch your playbook.

[ Download now: A system administrator's guide to IT automation. ]

Using a variable

To use a variable in a playbook, you reference it in curly braces ({{ }}). For example, this simple playbook uses the debug module to print the value of a variable to the terminal:

---
- hosts: localhost
  tasks:
    - name: echo a variable
      debug:
        msg: "{{ my_var }}"

Because the variable hasn't been defined, the play results in an error:

$ ansible-playbook ./example.yaml
[...]
fatal: [localhost]: FAILED! =>
{"msg": "The task includes an option with an undefined variable.
The error was: 'my_var' is undefined...

[ Get started with IT automation with the Ansible Automation Platform beginner's guide. ]

To define a variable dynamically when you run a playbook, use the --extra-vars option along with the key and value of the variable you want to define. In this example, the key is my_var because that's the string referenced in the playbook, and the value is any string you want the variable to contain.

$ ansible-playbook --extra-vars my_var=foo  ./example.yaml
[...]
TASK [echo a variable] ****************************
ok: [localhost] => {
    "msg": "foo"
}

PLAY RECAP ****************************************
localhost : ok=2    changed=0    failed=0 [...]

This time, the variable's value is passed into Ansible through the command option and successfully printed to the terminal.

Variables and Ansible

Ansible supports many ways of passing variables into scripts, generating variables within scripts, and even processing variables as part of a playbook's tasks. For more information on how to process variables in a playbook using filters, read 2 practical ways to use filters to manipulate data in Ansible and Network configuration with Ansible filters.


Sobre o autor

Seth Kenlon is a Linux geek, open source enthusiast, free culture advocate, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he likes to design games and hack on code (also not necessarily exclusive of one another).

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

Navegue por canal

automation icon

Automação

Últimas novidades em automação de TI para empresas de tecnologia, equipes e ambientes

AI icon

Inteligência artificial

Descubra as atualizações nas plataformas que proporcionam aos clientes executar suas cargas de trabalho de IA em qualquer ambiente

open hybrid cloud icon

Nuvem híbrida aberta

Veja como construímos um futuro mais flexível com a nuvem híbrida

security icon

Segurança

Veja as últimas novidades sobre como reduzimos riscos em ambientes e tecnologias

edge icon

Edge computing

Saiba quais são as atualizações nas plataformas que simplificam as operações na borda

Infrastructure icon

Infraestrutura

Saiba o que há de mais recente na plataforma Linux empresarial líder mundial

application development icon

Aplicações

Conheça nossas soluções desenvolvidas para ajudar você a superar os desafios mais complexos de aplicações

Virtualization icon

Virtualização

O futuro da virtualização empresarial para suas cargas de trabalho on-premise ou na nuvem