#最近一直在學習薑汁老師的Ansible教程,還特地去看了《安德的遊戲》。html
寫這篇算是一個學習和實驗的記錄,就怕萬一下次工做須要的時候本身已經忘得一乾二淨了,白給。#python
首先是Ansible控制主機的安裝,這裏有個坑。當咱們執行playbook的時候 會有Error :xxxxx,Msg:xxxxx,會提示你install ncclient。由於netconf是Py3的模塊,Ansible默認使用的是Py2.7。
git
附上解決辦法的連接:https://acozine.github.io/html/reference_appendices/python_3_support.htmlgithub
先去了解下juniper.junos:app
Contents:ssh
甚至會發現有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設備!