Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问,这个支持其他端口么? #7

Open
pdudo opened this issue Jun 4, 2019 · 4 comments
Open

请问,这个支持其他端口么? #7

pdudo opened this issue Jun 4, 2019 · 4 comments

Comments

@pdudo
Copy link

pdudo commented Jun 4, 2019

我刚看了下,默认仅支持22端口对吧,请问,支持其他端口么?

@pdudo
Copy link
Author

pdudo commented Jun 4, 2019

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.
# 

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

@Joshuapy
Copy link
Owner

Joshuapy commented Jun 5, 2019

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

@pdudo
Copy link
Author

pdudo commented Jun 6, 2019

大胸弟,不行呀,我试了一下,还是有问题,我尝试修改了一下你的代码,让它输出一些信息出来,如下:
修改路径: 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' 这个有点蒙蔽,看不太懂

@Joshuapy
Copy link
Owner

Joshuapy commented Jul 4, 2019

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"}
}
}
}

参考下这个格式

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants