无法使用ansible库存文件,因为它是可执行的

Ste*_*ley 6 virtualbox shared-directory chmod ansible

我正在尝试运行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`.
Run Code Online (Sandbox Code Playgroud)

我相信这是因为我正在使用Virtual Box和共享文件夹,它们强制我的所有文件都是ug + rwx.并且vbox不允许更改共享文件夹的权限(至少来自Windows的共享文件夹,这是我的情况)

有没有办法让Ansible运行这个文件?我可以看到几个选项:

  1. 编辑hosts-prod成为可执行文件.我不知道这涉及到什么(显然是Ansible的新手).
  2. 在Ansible中设置配置选项,告诉它不要将此文件作为可执行文件运行 - 只需将其视为静态配置文件即可.我找不到这样做的选项,所以我怀疑这是不可能的.
  3. 将文件移到共享文件夹之外:在我的情况下不是一个选项.
  4. 你的好主意..

所有的帮助/想法赞赏!

实际的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
Run Code Online (Sandbox Code Playgroud)

hka*_*iti 5

可执行库存被解析为JSON而不是ini文件,因此您可以将其转换为输出JSON的脚本.最重要的是,Ansible将一些参数传递给一个简单的"猫"是不够的:

#!/bin/bash
cat <<EOF
{
 "_meta": {
   "hostvars": {
     "host1": { "some_var": "value" }
   }
 },
 "hostgroup1": [
   "host1",
   "host2"
 ]
 ...
}
EOF
Run Code Online (Sandbox Code Playgroud)

不如简单的"猫"优雅,但应该工作.


Ste*_*ley 5

@ hkariti的答案是第一个并且最接近原始问题.我最终将配置文件完全重写为Ruby脚本,并且工作正常.我以为我会在这里分享这些代码,因为找到Ansible动态库存文件的完整示例对我来说并不是一件容易的事.该文件与静态文件的不同之处在于如何将变量与清单中的计算机列表相关联(使用_meta标记).

#!/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
Run Code Online (Sandbox Code Playgroud)