无法使用 ansible 清单文件,因为它是可执行的 [英] Can't use ansible inventory file because it is executable
问题描述
我正在尝试运行 Ansible 库存文件 ansible -i hosts-prod all -u root -m ping
并且失败并显示以下消息:
I am trying to run an Ansible inventory file ansible -i hosts-prod all -u root -m ping
and it is failing with this message:
ERROR: The file hosts-prod is marked as executable,
but failed to execute correctly. If this is not supposed
to be an executable script, correct this with
`chmod -x hosts-prod`.
我相信这是因为我正在使用 Virtual Box 和共享文件夹,这迫使我的所有文件都使用 ug+rwx.并且 vbox 不允许更改共享文件夹的权限(至少是来自 Windows 的共享文件夹,这是我的情况)
I believe this is because I am using Virtual Box and shared folders which is forcing all my files to ug+rwx. And vbox does not permit changing permissions on shared folders (at least shared folders coming from Windows which is my situation)
有没有办法让 Ansible 运行这个文件?我可以看到几个选项:
Is there a way to allow Ansible to run this file? I can see several options:
- 编辑
hosts-prod
成为可执行文件.我不知道这其中涉及到什么(显然是 Ansible 的新手). - 在 Ansible 中设置一个配置选项,告诉它不要将此文件作为可执行文件运行 - 只需将其视为静态配置文件即可.我找不到执行此操作的选项,因此我怀疑这是不可能的.
- 将文件移到共享文件夹之外:就我而言,这不是一个选项.
- 你的好主意..
- Edit
hosts-prod
to become an executable file. I don't know what's involved in this (being new to Ansible, obviously). - Set a configuration option in Ansible to tell it not to run this file as executable - just treat it as the static configuration file it is. I can't find an option to do this, so I suspect it's not possible.
- Move the file outside of shared-folders: not an option in my case.
- Your better idea..
感谢所有帮助/想法!
实际的 hosts-prod
配置文件如下所示,因此欢迎任何有关使其在内部可执行的提示:
The actual hosts-prod
config file looks as follows, so any tips on making it internally executable would be welcome:
web01 ansible_ssh_host=web01.example.com
db01 ansible_ssh_host=db01.example.com
[webservers]
web01
[dbservers]
db01
[all:vars]
ansible_ssh_user=root
推荐答案
可执行清单被解析为 JSON 而不是 ini 文件,因此您可以将其转换为输出 JSON 的脚本.最重要的是,Ansible 向它们传递一些参数给一个简单的猫"是不够的:
Executable inventories are parsed as JSON instead of ini files, so you can convert it to a script that outputs JSON. On top of that, Ansible passes some arguments to them to a simple 'cat' isn't enough:
#!/bin/bash
cat <<EOF
{
"_meta": {
"hostvars": {
"host1": { "some_var": "value" }
}
},
"hostgroup1": [
"host1",
"host2"
]
...
}
EOF
不像简单的猫"那么优雅,但应该可以.
Not as elegant as a simple 'cat', but should work.
这篇关于无法使用 ansible 清单文件,因为它是可执行的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!