Virtualbox Can';不要使用ansible库存文件,因为它是可执行的
我正在尝试运行Ansible资源清册文件Virtualbox Can';不要使用ansible库存文件,因为它是可执行的,virtualbox,chmod,ansible,shared-directory,Virtualbox,Chmod,Ansible,Shared Directory,我正在尝试运行Ansible资源清册文件Ansible-I hosts prod all-u root-m ping,失败时显示以下消息: 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`.
Ansible-I hosts prod all-u root-m ping
,失败时显示以下消息:
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`.
我相信这是因为我使用的是虚拟盒和共享文件夹,这迫使我所有的文件到ug+rwx。并且vbox不允许更改共享文件夹的权限(至少是来自Windows的共享文件夹,这是我的情况)
是否有方法允许Ansible运行此文件?我可以看到几个选项:
hosts prod
,使其成为可执行文件。我不知道这其中涉及到什么(显然,对于Ansible来说是新的)hosts prod
配置文件如下所示,因此欢迎提供有关使其在内部可执行的任何提示:
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还将一些参数传递给了一个简单的“cat”,这是不够的:
#!/bin/bash
cat <<EOF
{
"_meta": {
"hostvars": {
"host1": { "some_var": "value" }
}
},
"hostgroup1": [
"host1",
"host2"
]
...
}
EOF
#/bin/bash
cat@hkariti的答案是第一个也是最接近原始问题的答案。最后,我将配置文件完全重新写入一个Ruby脚本,效果很好。我想我应该在这里分享这段代码,因为找到Ansible动态清单文件的完整示例对我来说并不容易。在如何将变量与清单中的机器列表关联(使用_meta标记)方面,该文件与静态文件不同
我喜欢这种想法!这对我不起作用-当我尝试此策略时,会收到以下输出/错误消息:steve@Zorin-VM:/media/sf_devdrive/learntagic/devops/ansible$ansible-i hosts prod all-u root-m ping/bin/cat:unrecogned option'--列出'Try'/bin/cat--help'了解更多信息。错误:无法分析可执行资源清册脚本结果:{'msg':'','failed':True,'parsed':False}
似乎正在尝试将“-list”传递给cat?为什么会这样?我不知道是什么反应——列表?Python没有。可能ansible可执行文件必须响应某些CLI参数选项?非常类似于
#!/usr/bin/env ruby
# this file must be executable (chmod +x) in order for ansible to use it
require 'json'
module LT
module Ansible
def self.staging_inventory
{
local: {
hosts:["127.0.0.1"],
vars: {
ansible_connection: "local"
}
},
common: {
hosts: [],
children: ["web", "db"],
vars: {
ansible_connection: "ssh",
}
},
web: {
hosts: [],
children: ["web_staging"]
},
db: {
hosts: [],
children: ["db_staging"]
},
web_staging: {
hosts: ["webdb01-ci"],
vars: {
# server specific vars here
}
},
db_staging: {
hosts: ["webdb01-ci"]
}
}
end
end
end
# ansible will pass "--list" to this file when run from command line
# testing for --list should let us require this file in code libraries as well
if ARGV.find_index("--list") then
puts LT::Ansible::staging_inventory.to_json
end