与Cisco设备的Ansible Python API连接

时间:2018-05-15 15:37:57

标签: python ansible

我使用简单的playbook从cisco路由器检索OSPF信息。 { "rules": { "pizzerie": { ".read" : true, ".write": true } } } 完美无缺。 我试图从python运行ansible-playbook,并且因为这样的错误而失败

ansible-playbook /etc/ansible/showrun-routers.yml -vvv

这是我的Python API输出

    TASK [It's time for MD5 checking on OSPF interfaces. Let's download full show run] *****  
<IP address> using connection plugin network_cli (was local) 
<IP address> starting connection from persistent connection plugin 
fatal: [IP address]: FAILED! => {"msg": "/usr/bin/python: can't open file 'ansible-connection': [Errno 2] No such file or directory\n"}

我试图比较ansible-playbook和python脚本的详细输出,他们都使用network_cli模块 不可用的播放书输出

#!/usr/bin/env python

import os
import sys
from collections import namedtuple

from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
from ansible.playbook.play import Play
from ansible.plugins.callback import CallbackBase
from ansible.vars.manager import VariableManager
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.utils.display import Display
display = Display(verbosity=7)
loader = DataLoader()

inventory = InventoryManager(loader=loader, sources='/etc/ansible/hosts')
variable_manager = VariableManager(loader=loader, inventory=inventory)
playbook_path = '/etc/ansible/showrun-routers.yml'

if not os.path.exists(playbook_path):
    print '[INFO] The playbook does not exist'
    sys.exit()

Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check','diff'])
options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='local', module_path='/usr/lib/python2.7/dist-packages/ansible', forks=100, remote_user=None, private_key_file=None, ssh_common_args='', ssh_extra_args='', sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=True, check=False, diff=False)

variable_manager = VariableManager(loader=loader, inventory=inventory)
passwords = {}

pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)

results = pbex.run()

Python API输出 在我的问题的顶部被提到。 所以我无法理解实际python在提出错误时的意思是无法打开文件'ansible-connection'。你对这个错误有什么建议吗? 感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我刚刚在ansible-connection文件中添加了一个符号链接 / etc / ansibleinpython#ansible-connection - &gt;的/ usr /斌/ ansible连接