Ansible – Adding variables in main playbook. Or importing from a different playbook

Ansible_Logo

Variables hold values that can be used when playbooks are executed. It can be defined in:

  • Inventory file (host scope)
  • Playbook (play scope)
  • Defined globally (global scope)

Play scope

In the below example variables are defined in the playbook

[root@centos9vm ~]# cat shijuplaybook.yml

==== ==== ==
– – –
– name: shiju sample playbook
hosts: testGRP
vars:
– theUser: shiju400
tasks:
– name: Add a user shiju100
ansible.builtin.user:
name: “{{ theUser }}”
state: present

==== ==== ==
[root@centos9vm ~]# ansible-navigator run -m stdout shijuplaybook.yml –syntax-check
playbook: /root/shijuplaybook.yml

[root@centos9vm ~]# ansible-navigator run -m stdout shijuplaybook.yml