Ansible配制Juniper设备

   #最近一直在学习姜汁老师的Ansible教程,还特意去看了《安德的游戏》。

    写这篇算是一个学习和实验的记录,就怕万一下次工作需要的时候自己已经忘得一干二净了,白给。#

    

    首先是Ansible控制主机的安装,这里有个坑。当我们执行playbook的时候 会有Error :xxxxx,Msg:xxxxx,会提示你install ncclient。因为netconf是Py3的模块,Ansible默认使用的是Py2.7。

     附上解决办法的链接:https://acozine.github.io/html/reference_appendices/python_3_support.html


  先去了解下juniper.junos:

Juniper.junos Ansible Modules

Contents:

  甚至会发现有HA的模块。facts用于收集junos系统信息,config用来今天帮助我们做配置。


    进入到config模块,可以看到模块的简介,选项和例子。可以执行包括:loading or rolling back, checking, diffing, retrieving, and committing the configuration 这么多功能,我们先用用最简单的commit。在使用之前请先安装好eznc。

    正式开始配置:

    把host和var写入到hosts文件里:

    [ex3300]

    192.168.11.169

    [ex3300:vars]

    ansible_connection=network_cli

    #ansible_connection=netconf

    ansible_network_os=junos

    ansible_user=netops

    ansible_password=juniper123


    准备配置文件:

    more junos_config_vars/vsrx_vars_ex3300.yaml 

    ---

     vsrx_config_ex3300:

      - set routing-options static route 2.2.2.2/32 discard

    实验用了Srx320 19.1R3,Vsrx15.1R和Ex3300 12.3R9版本测试。这里的文件名字写混乱了 关系不大。


    设备初始化:

    设备的MGT接口 ssh/netconf和ssh用户:

    set system root-authentication encrypted-password "$1$d2G1wOPF$Lth.0XBee52ROKcFwayxr/"
    set system login user netops uid 203
    set system login user netops class super-user
    set system login user netops authentication encrypted-password "$1$HFIwO3Kj$OQ9IDKraR5rYSns2mRXJh/"
    set system services ftp
    set system services ssh root-login allow
    set system services netconf ssh port 830
    set interfaces me0 unit 0 family inet address 192.168.11.169/24 


    写剧本:

    配置变量采用导入变量文件的方式,用vars_files参数来定位。

    vars_files:

        - junos_config_vars/vsrx_vars_ex3300.yaml


     编辑juniper_junos_config的options:

     juniper_junos_config:

          config_mode: 'exclusive' #默认就是exclusive

          load: 'set'  #set或者merge模式

          lines: "{{ vsrx_config_ex3300 }}" #包含配置的字符串列表

          commit: yes #也不check或者干嘛了直接commit


    运行。Ansible-playbook junos_config_ex3300.yaml 


    PLAY [configure SRX] ****************************************************************************************


    TASK [configure infomation :] *******************************************************************************

    ok: [192.168.11.169] => {

        "vsrx_config_ex3300": [  #debug输出var变量也就是配置文件。

            "set routing-options static route 3.3.3.3/32 discard"

        ]

    }


    TASK [load configure into vSRX] *****************************************************************************

    这里会有一大段告警 但是不影响结果 暂时不理会(其实我去官网看了没看懂)。

    changed: [192.168.11.169]


    TASK [print configure results] ******************************************************************************

    ok: [192.168.11.169] => { #changed == true 从而debug msg

        "msg": "vSRX configure completed thanks"

    }


    PLAY RECAP **************************************************************************************************

    192.168.11.169             : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


    整理下思路:设备初始化;Ansible的host和var;配置变量(变量文件的方式);Playbook(可以做的事情太多了 这次使用的有 定位变量的文件位置,配置前先输出配置,利用模块导入配置到相应的主机,配置成功changed以后输出提示)。


    在官网的例子里面有一个是直接这么来的:

    lines:

      - 'set system services netconf ssh'

     


   最后回到设备上去看下配置compare | rollback 1:

 +    route 3.3.3.3/32 discard 

    大功告成,简单的利用Ansible配置Juniper设备!

猜你喜欢

转载自blog.51cto.com/13582804/2431455
今日推荐