在playbook中访问Ansible主机变量 - 错误

时间:2016-03-31 06:20:17

标签: ansible ansible-playbook

我有一个简单的ansible playbook,包含以下数据

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 }}"

当我使用命令ansible-playbook -i inventory main.yaml运行playbook时,出现以下错误

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

任何帮助表示赞赏。谢谢。

1 个答案:

答案 0 :(得分:1)

看起来问题是包含

[targets]
admin ansible_connection=local

根据ansible documentationYou can also select the connection type and user on a per host basis

因此,将库存文件更改为

[targets]
localhost ansible_connection=local

应解决问题