使用外部vars_files添加主机名

时间:2018-12-28 10:56:34

标签: ansible ansible-tower

我想在主机名中添加外部文件。

我已经在我的剧本主vars_files中使用过添加主机名,但似乎没有用。

我需要在ansible项目中将主机名添加到外部文件中。
我无法使用主机文件和组,因为我使用的是Ansible Tower (3.2.2),并且它已经拥有了自己的清单。

请注意,由于某些原因,我无法将我的vars_files添加到Ansible的项目中。

我已经尝试添加绝对路径。
编辑:请注意,以下错误消息仅通过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 个答案:

答案 0 :(得分:0)

“从外部文件添加主机名” ,您可能需要使用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"