Giter VIP home page Giter VIP logo

Comments (4)

pdudo avatar pdudo commented on July 24, 2024
branch: for-2.4
python: 2.7
ansible: 2.4

我看 myinventory.py中介绍的 使用如下方法:
""" python
this is my ansible inventory object.
支持三种数据类型的主机信息:
- 字符串形式: "1.1.1.1, 2.2.2.2", "1.1.1.1"
- 列表/集合形式: ["1.1.1.1", "2.2.2.2"], {"1.1.1.1", "2.2.2.2"}
- 字典形式: {
"group1": {
"hosts": [{"hostname": "10.10.10.10", "port": "22",
"username": "test", "password": "mypass"}, ...]
"vars": {"var1": value1, "var2": value2, ...}
}
}
"""

于是乎我修改我的python代码:

# cat ad_hoc.py 
#!/usr/bin/env python

from pprint import pprint
from ansible2_myAPI.runner import Runner

runner = Runner(
	module_name="shell",
	module_args="df -h",
	pattern="all",
	hosts={
            "group1": {
                "hosts": [{"hostname": "10.10.10.10", "port": "23",
                            "username": "test", "password": "mypass"}]
            }
        }
)

result = runner.run()

print(result)

# 

修改完毕后,报错如下:

# ./ad_hoc.py 
Traceback (most recent call last):
  File "./ad_hoc.py", line 13, in <module>
    "username": "test", "password": "mypass"}]
  File "/root/python/ansible2_myAPI/runner.py", line 118, in __init__
    self.inventory = MyInventory(host_list=hosts)
  File "/root/python/ansible2_myAPI/myinventory.py", line 33, in __init__
    super(MyInventory, self).__init__(self.loader, host_list)
  File "/usr/lib/python2.7/site-packages/ansible/inventory/manager.py", line 145, in __init__
    self.parse_sources()
  File "/root/python/ansible2_myAPI/myinventory.py", line 50, in parse_sources
    parsed = self.parse_source(self._sources)
  File "/root/python/ansible2_myAPI/myinventory.py", line 71, in parse_source
    raise AnsibleParserError("No plugin could parse your data.")
ansible.errors.AnsibleParserError: No plugin could parse your data.
# 

请问,修改端口应该怎么样修改呢?

from ansible2_myapi.

Joshuapy avatar Joshuapy commented on July 24, 2024

组变量里不能放置密码,这里应该是我写错了, 你用sshkey的方式执行下应该可以执行通过。
应该和端口没关系

from ansible2_myapi.

pdudo avatar pdudo commented on July 24, 2024

大胸弟,不行呀,我试了一下,还是有问题,我尝试修改了一下你的代码,让它输出一些信息出来,如下:
修改路径: ansible2_myAPI/myinventory.py 行数: 71开始

    def parse_sources(self, cache=False):
        """ 
        覆盖父类的解析方法,因为父类的该方法要:
            1. 遍历内容
            2. 路径字符串处理
        而这里的解析只需要直接解析就行.
        """

        self._setup_inventory_plugins()

        parsed = self.parse_source(self._sources)
        if parsed:
            self._inventory.reconcile_inventory()

        self._inventory_plugins = []

    def parse_source(self, source):
        parsed = False

        if not self._inventory_plugins:
            self._setup_inventory_plugins()

        for plugin in self._inventory_plugins:
            if plugin.verify_file(source):
                print (type(plugin))
                try:
                    plugin.parse(self._inventory, self._loader, source)
                    parsed = True
                    break
                except Exception as e:
                    print ("The Program Exception ... ")
                    print (e) 
                    pass
        else:
            raise AnsibleParserError("No plugin could parse your data.")

        return parsed

我的客户端python测试脚本

# cat ad_hoc.py 
#!/usr/bin/env python

from pprint import pprint
from ansible2_myAPI.runner import Runner

runner = Runner(
	module_name="shell",
	module_args="df -h",
	pattern="all",
	hosts={
            "group1": {
		"hosts": [{
			"hostname": "127.0.0.1",
		}]
            }
        }
)

result = runner.run()

pprint(result)
# 

报错如下:

# ./ad_hoc.py 
<class 'ansible2_myAPI.myinventory.InventoryDictPlugin'>
The Program Exception ... 
unhashable type: 'dict'
Traceback (most recent call last):
  File "./ad_hoc.py", line 13, in <module>
    "hostname": "127.0.0.1",
  File "/root/python/ansible2_myAPI/runner.py", line 118, in __init__
    self.inventory = MyInventory(host_list=hosts)
  File "/root/python/ansible2_myAPI/myinventory.py", line 33, in __init__
    super(MyInventory, self).__init__(self.loader, host_list)
  File "/usr/lib/python2.7/site-packages/ansible/inventory/manager.py", line 145, in __init__
    self.parse_sources()
  File "/root/python/ansible2_myAPI/myinventory.py", line 50, in parse_sources
    parsed = self.parse_source(self._sources)
  File "/root/python/ansible2_myAPI/myinventory.py", line 74, in parse_source
    raise AnsibleParserError("No plugin could parse your data.")
ansible.errors.AnsibleParserError: No plugin could parse your data.
# 

最后报错 unhashable type: 'dict' 这个有点蒙蔽,看不太懂

from ansible2_myapi.

Joshuapy avatar Joshuapy commented on July 24, 2024

hosts = {
"group1": ["1.1.1.1", "2.2.2.2"], #格式1
"group2": { #格式2
"hosts": ["1.1.1.1", "2.2.2.2"],
"vars":{"some_vars": "some_values"},
"children": ["other_group"],
},
"3.3.3.3": { # 格式3
"some_var2": "some_value2",
"foo": "bar"
}
"_meta": { # 主机变量
"hostvars": {
"1.1.1.1": {"var1": "value1"},
"2.2.2.2": {"var2": "value2"},
"3.3.3.3": {"var3": "value3"}
}
}
}

参考下这个格式

from ansible2_myapi.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.