gpt4 book ai didi

python - Ansible 动态 list - "([Errno 2] No such file or directory:"

转载 作者:行者123 更新时间:2023-12-04 09:06:43 24 4
gpt4 key购买 nike

情况:尝试在 Ansible 中加载基本的动态 list python 文件时遇到问题。
背景:当我执行 python test.py 时,它会输出列表,但是在 ansible 中它会失败
评估:我已经尝试了很多其他示例文件,但没有一个有效,这是我能做到的最简单的
推荐 : 有人可以指点我正确的方向吗?这与 ansible.cfg 有关系吗?

vagrant@cd98f180bc88 /vagrant/adc-cmdb-inventory/enviroments $ python test.py --list
{"all": {"hosts": ["192.168.28.71", "192.168.28.72"], "vars": {"ansible_python_interpreter": "/usr/bin/python3", "ansible_ssh_private_key_file": "~/.vagrant.d/insecure_private_key", "example_variable": "value", "ansible_user": "vagrant"}}, "_me
ta": {"hostvars": {"192.168.28.72": {"host_specific_var": "bar"}, "192.168.28.71": {"host_specific_var": "foo"}}}}

问题:
使用的命令:
ansible -i test.py all -m ping --list-host
[WARNING]:  * Failed to parse /vagrant/adc-cmdb-inventory/enviroments/test.py with script plugin: problem running /vagrant/adc-cmdb-inventory/enviroments/test.py --list ([Errno 2] No such file or directory: '/vagrant/adc-cmdb-
inventory/enviroments/test.py': '/vagrant/adc-cmdb-inventory/enviroments/test.py')

[WARNING]: * Failed to parse /vagrant/adc-cmdb-inventory/enviroments/test.py with ini plugin: /vagrant/adc-cmdb-inventory/enviroments/test.py:4: Expected key=value host variable assignment, got: os

[WARNING]: Unable to parse /vagrant/adc-cmdb-inventory/enviroments/test.py as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

hosts (0):


测试.py
#!/usr/bin/python
# -*- coding:utf-8 -*-

import os
import sys
import argparse
import json

class ExampleInventory(object):

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

# Called with `--list`.
if self.args.list:
self.inventory = self.example_inventory()
# Called with `--host [hostname]`.
elif self.args.host:
# Not implemented, since we return _meta info `--list`.
self.inventory = self.empty_inventory()
# If no groups or vars are present, return empty inventory.
else:
self.inventory = self.empty_inventory()

print(json.dumps(self.inventory))

# Example inventory for testing.
def example_inventory(self):
return {
'all': {
'hosts': ['192.168.28.71', '192.168.28.72'],
'vars': {
'ansible_user': 'vagrant',
'ansible_ssh_private_key_file':
'~/.vagrant.d/insecure_private_key',
'ansible_python_interpreter':
'/usr/bin/python3',
'example_variable': 'value'
}
},
'_meta': {
'hostvars': {
'192.168.28.71': {
'host_specific_var': 'foo'
},
'192.168.28.72': {
'host_specific_var': 'bar'
}
}
}
}

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

# 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.
ExampleInventory()

最佳答案

隐形线结局!
我最终安装了 dos2unix 并转换了每行末尾都有 CR 和 LF 标签的文件。
用 dos2unix 删除 CF 行,它第一次工作。
https://imgur.com/AN8ACWC

关于python - Ansible 动态 list - "([Errno 2] No such file or directory:",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63428180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com