Ansible - 访问库存别名

Pro*_*e85 30 ansible

有一个库存文件,如:

[my_hosts]
my_host ansible_ssh_host=123.123.123.123
my_host2 ansible_ssh_host=234.234.234.234
Run Code Online (Sandbox Code Playgroud)

我想在我的模板中收集一些调试信息.

我试图收集事实ansible -m setup my_host.变量ansible_hostname,HOSTNAMEHOST包含机器的主机名,即echo $HOSTNAME与我的ansible别名不同.

Pro*_*e85 61

我正在搜索的变量是一个内置功能: inventory_hostname

有关inventory_hostnameinventory_hostname_short可以从Magic变量章节和如何访问有关其他主机的信息中找到的Ansible文档.

原始问题:https://groups.google.com/forum/#!topic / ans-project/Oa5YXjHecIw


Ric*_*ico 13

你可以简单地使用{{ ansible_ssh_host }}例如:

库存:

[my_hosts]
my_host ansible_ssh_host=127.0.0.1 my_host_alias=my_host
Run Code Online (Sandbox Code Playgroud)

剧本:

---
- name: My Good playbook
  user: ubuntu
  hosts: all

  tasks:
    - name: My message
      debug: msg="Myhost is {{ ansible_ssh_host }}"

    - name: My message bogus
      debug: msg="My host alias is {{ my_host_alias }}"
Run Code Online (Sandbox Code Playgroud)

执行:

$ ansible-playbook -i inventory play.yml

PLAY [My Good playbook] *******************************************************

GATHERING FACTS ***************************************************************
ok: [my_host]

TASK: [My message] ************************************************************
ok: [my_host] => {
    "msg": "Myhost is 127.0.0.1"
}

TASK: [My message bogus] ******************************************************
ok: [my_host] => {
    "msg": "My host alias is my_host"
}

PLAY RECAP ********************************************************************
my_host                    : ok=3    changed=0    unreachable=0    failed=0
Run Code Online (Sandbox Code Playgroud)