cucucool
cucucool

Reputation: 3877

Accessing Ansible host variables in playbook - error

I have a simple ansible playbook with the following data

inventory
----------
[admin]
127.0.0.1

[admin:vars]
service_ip=12.1.2.1

[targets]
admin ansible_connection=local

main.yaml
----------
---
- hosts: admin
  roles:
    - admin
  tags: admin

roles/admin/tasks/main.yaml
---------------------------
- debug: msg="{{ service_ip }}"

when I run the playbook using the command, ansible-playbook -i inventory main.yaml, I get the following error

PLAY [admin] ****************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [admin]

TASK: [admin | debug msg="{{ service_ip }}"] ********************************** 
fatal: [admin] => One or more undefined variables: 'service_ip' is undefined

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/Users/karthi/main.yaml.retry

admin                      : ok=1    changed=0    unreachable=1    failed=0

Any help appreciated. Thanks.

Upvotes: 1

Views: 638

Answers (1)

sbochins
sbochins

Reputation: 198

It looks like the issue is the inclusion of

[targets]
admin ansible_connection=local

as per the ansible documentation, You can also select the connection type and user on a per host basis.

So, changing your inventory file to have

[targets]
localhost ansible_connection=local

should fix the issue

Upvotes: 1

Related Questions