【问题标题】:Add hostnames with external vars_files使用外部 vars_files 添加主机名
【发布时间】:2018-12-28 10:56:34
【问题描述】:

我想用外部文件添加我的主机名。

我已经在我的剧本大师vars_files 中使用了我的主机名,但它似乎不起作用。

我需要在我的 ansible 项目的外部文件中添加主机名。
我不能使用主机文件和组,因为我使用Ansible Tower (3.2.2) 并且它已经有他自己的库存。

请注意,由于某些原因,我不能在我的 Ansible 项目中添加我的 vars_files

我已经尝试添加绝对路径。
编辑:请注意,以下错误消息仅通过 Ansible Tower 执行。
使用命令行,它工作正常。

---

- hosts:  "{{ hostnames }}"

# include vars from file
  vars_files:
    # this line doesn't work
    - /project/ansible/home/ansible/ansible_scripts/vars/dev.yml
      # this line doesn't work
    # - /project/ansible/home/ansible/ansible_scripts/vars/dev.yml
      # this line doesn't work
    # - ../../../ansible/home/ansible/ansible_scripts/vars/dev.yml
    # this line works fine
    # - vars/dev.yml

# play roles
  roles: 
    - check_info_servers

错误是:

ERROR! vars file project/ansible/home/ansible/ansible_scripts/vars/dev.yml was not found on the Ansible Controller.
If you are using a module and expect the file to exist on the remote, see the remote_src option

【问题讨论】:

  • 1) 您使用的是 Ansible Tower 还是 AWX? 2) 它是否作为 Docker 容器安装在您的服务器上?
  • 我正在使用 Ansible Tower
  • 不确定,但可能是绝对路径上的错误是由于 Tower virtualenv
  • 也许你是对的。我仍然是 ansible Tower 的菜鸟 :(

标签: ansible ansible-tower


【解决方案1】:

“从外部文件添加主机名”,您可能需要使用 add_host 模块。

例如:

> cat /scratch/dev.yml
my_hosts:
  - host1.example.com
  - host2.example.com
  - host3.example.com


> cat test.yaml
- hosts: localhost
  vars_files:
    - /scratch/dev.yml
  tasks:
    - add_host:
        name: "{{ item }}"
      loop: "{{ my_hosts }}"
    - debug: msg="{{ item }}"
      with_inventory_hostnames:
        - all


> ansible-playbook test.yaml | grep msg
    "msg": "host3.example.com"
    "msg": "host1.example.com"
    "msg": "host2.example.com"

【讨论】:

  • 与 ansible Tower 相同的问题,但 add_hosts 模块在命令行下工作正常。也许不可能通过 ansible Tower 从外部项目添加变量...
猜你喜欢
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多