ansible - 使用外部 vars_files 添加主机名
问题描述
我想用外部文件添加我的主机名。
我在我的剧本大师vars_files
中使用过添加我的主机名,但它似乎不起作用。
我需要在我的 ansible 项目的外部文件中添加主机名。
我不能使用主机文件和组,因为我使用Ansible Tower (3.2.2)并且它已经有他自己的清单。
请注意,由于某些原因,我不能将 my 添加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
解决方案
要“从外部文件添加主机名” ,您可能需要使用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"
推荐阅读
- python - 通过 Sympy 中的 sympify 方法定义向量
- java - 在另一个活动中从 TextView 中获取值
- mongodb - mongodb 的 $facet 返回完整排序的文档而不是基于匹配的计数
- html - 试图在页面中间放置一个矩形容器
- google-cloud-platform - 有什么方法可以利用 Cloud Function auth env vars 与 Google Drive 交互?
- pandas - 有没有更快的方法来使用 pandas 连接巨大的数据帧(40GB)
- docker - Google App Engine:为 Scrapy + Splash 加载另一个 Docker 映像
- reactjs - Material-ui - 树状视图 展开全部
- excel - 使用 VBA 在 Excel 2016 中自定义功能区
- python - 转义字符串中的十六进制字符