Ansible – Variables – Adding variables in host file

Ansible_Logo

In the below example variables are defined in the host inventory file.

[root@centos9vm ~]# cat inventory

=== === ===

192.168.132.129
192.168.132.130
servera.example.com

[webservers]
servera.example.com
serverb.example.com

[dbservers]
serverc.example.com
serverb.example.com
servera.example.com

[myGroups:children]
webservers
dbservers

[testGRP]
192.168.132.130
[testGRP:vars]
theUser = shiju100

=== === ===

[root@centos9vm ~]# cat shijuplaybook.yml
== ===== ===
– – –
– name: shiju sample playbook
    hosts: testGRP
    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

PLAY [shiju sample playbook] ***************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.132.130]

TASK [Add a user shiju100] *****************************************************
changed: [192.168.132.130]

PLAY RECAP *********************************************************************
192.168.132.130 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos9vm ~]#