我确认了Ansible的Junos模块的连接实现,并做了以下记录
我簡單地確認了一下對Ansible的Junos模組連接實現方法感興趣的備註。
1. Junos模块的连接方式
从Ansible2.5开始,推荐使用以下连接方法。
-
- network_cli(ssh)
- netconf(xml over ssh)
请用中文概述以下内容(只需要一种选项):
http://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html 是关于Ansible在Junos平台上的用户指南。
我查证了 Ansible 的 Junos 模块中如何实现上述连接方式。
2. 检查实施情况
2-1. 连接处理
当执行 get_connection 方法时,如果是 cliconf(network_cli) ,将返回 Connection 类对象;如果是 netconf ,将返回 NetconfConnection 类对象。
(snip)
def get_connection(module):
if hasattr(module, '_junos_connection'):
return module._junos_connection
capabilities = get_capabilities(module)
network_api = capabilities.get('network_api')
if network_api == 'cliconf':
module._junos_connection = Connection(module._socket_path)
elif network_api == 'netconf':
module._junos_connection = NetconfConnection(module._socket_path)
else:
module.fail_json(msg='Invalid connection type %s' % network_api)
return module._junos_connection
def get_capabilities(module):
if hasattr(module, '_junos_capabilities'):
return module._junos_capabilities
capabilities = Connection(module._socket_path).get_capabilities()
module._junos_capabilities = json.loads(capabilities)
return module._junos_capabilities
(snip)
请帮我将以下链接转为中文:https://github.com/ansible/ansible/blob/57f6abdb8460f8501eb9668004f4d7722bda5570/lib/ansible/module_utils/network/junos/junos.py#L69
2-2. 关于可执行方法
2-2-1. cliconf方法中的方法
以下是可在 cliconf 中执行的方法列表。
[root@localhost dev]# grep 'def ' ansible-devel/lib/ansible/plugins/cliconf/junos.py | sed -e "s/^ *def //g" -e "s/:$//g"
get_text(self, ele, tag)
get_device_info(self)
get_config(self, source='running', format='text')
edit_config(self, command)
get(self, command, prompt=None, answer=None, sendonly=False)
commit(self, *args, **kwargs)
discard_changes(self)
get_capabilities(self)
compare_configuration(self, rollback_id=None)
2-2-2. netconf方法时的函数
[root@localhost dev]# grep 'def' ansible-devel/lib/ansible/plugins/netconf/junos.py | sed -e "s/.*def //g" -e "s/^ *(by .*//g" | sed -e "/^ *$/d" -e "s/:$//g"
get_text(self, ele, tag)
get_device_info(self)
execute_rpc(self, name)
load_configuration(self, *args, **kwargs)
get_capabilities(self)
guess_network_os(obj)
get_configuration(self, *args, **kwargs)
compare_configuration(self, *args, **kwargs)
halt(self)
reboot(self)
halt(self)
get(self, *args, **kwargs)
get_config(self, *args, **kwargs)
edit_config(self, *args, **kwargs)
commit(self, *args, **kwargs)
validate(self, *args, **kwargs)
discard_changes(self, *args, **kwargs)
3. 检查操作是否正确
我想要创建一个简单的Junos Ansible模块。
我将创建一个可以执行命令并获取结果的模块。
3-1. 模块
这个模块只是用来执行”show version”指令的。
#!/usr/bin/python
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.junos.junos import junos_argument_spec, get_connection
argument_spec = junos_argument_spec
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
conn = get_connection(module)
result = dict(changed=False)
result['cmd'] = conn.get(command="show version")
module.exit_json(**result)
get方法同时在network_cli和netconf中都得到了支持。
3-2. 配置手册
---
- name: Junos Connect TEST
hosts: junos
gather_facts: no
tasks:
- test_junos_module:
register: r
- debug: var=r
3-3. 库存
在这里我们将尝试使用 network_cli。
[junos]
192.168.0.60
[junos:vars]
ansible_connection=network_cli
ansible_network_os=junos
ansible_user=root
ansible_ssh_pass=secret
3-4. 进行
[root@localhost dev]# ls
inventory main.yml test_junos_module.py
[root@localhost dev]# ansible-playbook main.yml -i inventory --module-path .
PLAY [Junos Connect TEST] ******************************************************************************************************************************************************
TASK [test_junos_module] *******************************************************************************************************************************************************
ok: [192.168.0.60]
TASK [debug] *******************************************************************************************************************************************************************
ok: [192.168.0.60] => {
"r": {
"changed": false,
"cmd": "Model: vsrx\nJunos: 15.1X49-D130.6\nJUNOS Software Release [15.1X49-D130.6]",
"failed": false
}
}
PLAY RECAP *********************************************************************************************************************************************************************
192.168.0.60 : ok=2 changed=0 unreachable=0 failed=0
可以通过如此连接来进行处理。
最后
由于我理解了有关连接的实现图像,所以只需掌握 lxml 和 ncclient 的知识,就可以制作出自己的 Junos 模块(包括其他网络相关模块)了 🙂