将 ansible 清单文件存储在远程位置

将 ansible 清单文件存储在远程位置

我正在尝试找到一种方法来将 Ansible 清单文件存储在服务器本地空间以外的位置(例如 Azure)。这可以通过 Ansible 动态清单实现吗?我读过有关动态清单的文档,但对我来说,这有点令人困惑。可能是因为我这边缺乏知识。

简单来说,我的问题是:有没有办法将 ansible 清单文件存储在远程位置,并在运行剧本/角色时要求 ansible 从那里读取它?如果是这样,那是什么?

答案1

是的,这只能通过动态库存来实现。编写一个 Python 脚本,如下所示:

#!/usr/bin/env python

import os
import sys
import argparse
import json
import requests

class MyInventory(object):

  def __init__(self):
    self.inventory = {}
    self.read_cli_args()

    # Called with `--list`.
    if self.args.list:
      self.inventory = self.my_inventory()

    # Called with `--host [hostname]`.
    elif self.args.host:
      # Not implemented, since we return _meta info `--list`.
      self.inventory = self.host_inventory(self.args.host)

    # If no groups or vars are present, return an empty inventory.
    else:
      self.inventory = self.empty_inventory('no args')
        
    print(json.dumps(self.inventory, indent=2, sort_keys=True))

  def my_inventory(self):
    try:
      if "INVENTORY_ENDPOINT" in os.environ:
        url = os.environ.get('INVENTORY_ENDPOINT')
      else:
        url = 'https://myserver/inventory'

      if "INVENTORY_USERNAME" in os.environ:
        username = os.environ.get('INVENTORY_USERNAME')
      else:
        return self.empty_inventory('environment-variable INVENTORY_USERNAME not set')

      if "INVENTORY_PASSWORD" in os.environ:
        password = os.environ.get('INVENTORY_PASSWORD')
      else:
        return self.empty_inventory('environment-variable INVENTORY_PASSWORD not set')
      
      headers = {
        'Accept': 'application/json',
        'User-Agent': 'dynamic-inventory-script/v1.0',
        'Connection': 'close'
      }
      
      result = requests.get(url, timeout=5, auth=(username,password), headers=headers)
      result.encoding = 'utf-8'
      return json.loads(result.text)        
    except:
      return {}      

  # Host inventory for testing.
  def host_inventory(self, hostname):
    return {
      '_meta': {
        'hostvars': {}
      }
    }

  # Empty inventory for testing.
  def empty_inventory(self,message):
    return {
      '_meta': {
        'hostvars': {
          'mesage': message
        }
      }
    }

  # Read the command line args passed to the script.
  def read_cli_args(self):
    parser = argparse.ArgumentParser()
    parser.add_argument('--list', action = 'store_true')
    parser.add_argument('--host', action = 'store')
    self.args = parser.parse_args()

# Get the inventory.
MyInventory()

硬编码或系统环境变量的 URL 应该指向 JSON 格式的库存。

请注意 - 动态库存始终采用 JSON 格式,并且布局与 INI 或 YAML 文件不同。请参阅https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#developing-inventory-scripts

然后你可以通过以下方式调用 ansible-playbook

ansible-playbook -i inventory.py ...

请注意 - 清单脚本不能是 Python 脚本。bash 脚本也可以。只需要 JSON 输出。

#!/bin/sh

if [ -z "$INVENTORY_USERNAME" ]; then
    echo "{}"
    exit 0
fi

if [ -z "$INVENTORY_ENDPOINT" ]; then
    INVENTORY_ENDPOINT="https://myserver/inventory"
fi

curl -s GET "$INVENTORY_ENDPOINT" -H "accept: application/json" --user $INVENTORY_USERNAME:$INVENTORY_PASSWORD &> /dev/stdout

#eof

相关内容