Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

Pick the next available (unconfigured) logical unit to configure with ansible.

‎03-14-2019 09:21 AM

Hi,

 

I'm working on a project to automate our creation of IPSec and IKE configuration.  All of it seems pretty straight forward except how to configure the next available logical unit.  I need to assign a logical unit under st0 interface, how would I go about automating w/ ansible to pick the next available unit.  IE.  interface's st0.1 through st0.12 are in use, how could I get ansible to find that 13 should be the next used unit to configure.

 

Thanks,

Eric

1 REPLY 1
Junos Automation (Scripting)

Re: Pick the next available (unconfigured) logical unit to configure with ansible.

[ Edited ]
‎04-03-2019 07:33 AM

Hi Eric.

 

I was able to create ansible playbook to obtain highest configured number of unit on st0 interface. I don't do any checks for unit config (IP address, used in other parts of config - routing, security).

 

The number of unit is obtained from interfaces config (committed one) and saved to variable "last_unit" for further usage.

 

YAML playbook:

 

mat@ubuntu-autom:~/ansible$ cat playbook/SRX-st0-config.yml

---
- name: juniper_junos_config example
  hosts: SRX1
  roles:
    - Juniper.junos
  connection: local
  gather_facts: no


  tasks:
  - name: Retrieve config [edit interfaces st0] from SRX device
    juniper_junos_config:
        provider: "{{credential_jun}}"
        retrieve: "committed"
        filter: "<interfaces><interface><name>st0</name></interface></interfaces>"
        format: "xml"
        config_mode: "private"
    register: tunnels

  - name: Save highest unit number of st0 interface of SRX device to variable
    set_fact:
       last_unit: "{{ (tunnels.config_parsed.configuration.interfaces.interface.unit | last).name }}"

  - name: Print highest unit number on st0 interface of SRX device
    debug:
       msg: "Last unit on st0 is: {{ last_unit }}"

 

Script output:

mat@ubuntu-autom:~/ansible$ ansible-playbook playbook/SRX-st0-config.yml

PLAY [juniper_junos_config example] *****************************************************************************************************************************

TASK [Retrieve config [edit interfaces st0] from SRX device] ****************************************************************************************************
ok: [SRX1]

TASK [Save highest unit number of st0 interface of SRX device to variable] **************************************************************************************
ok: [SRX1]

TASK [Print highest unit number on st0 interface of SRX device] *************************************************************************************************
ok: [SRX1] => {
    "msg": "Last unit on st0 is: 11"
}

PLAY RECAP ******************************************************************************************************************************************************
SRX1                       : ok=3    changed=0    unreachable=0    failed=0

 

Device config:

labroot@vSRX-node0> show configuration interfaces st0 | display set
set interfaces st0 unit 0 family inet address 172.16.0.1/30
set interfaces st0 unit 1 family inet address 172.16.1.1/30
set interfaces st0 unit 2 family inet address 172.16.2.1/30
set interfaces st0 unit 3 family inet address 172.16.3.1/30
set interfaces st0 unit 4 family inet address 172.16.4.1/30
set interfaces st0 unit 5 family inet address 172.16.5.1/30
set interfaces st0 unit 6 family inet address 172.16.6.1/30
set interfaces st0 unit 7 family inet address 172.16.7.1/30
set interfaces st0 unit 8 family inet address 172.16.8.1/30
set interfaces st0 unit 9 family inet address 172.16.9.1/30
set interfaces st0 unit 10 family inet address 172.16.10.1/30
set interfaces st0 unit 11 family inet address 172.16.11.1/30

labroot@vSRX-node0> 

 

Tested with:

Ubuntu 18.04.1 LTS (Bionic Beaver)

Ansible 2.5.1

Juniper.junos  2.1.0 (Juniper Ansible modules from Galaxy)

Python 2.7.15rc1

vSRX 3.0  18.4R1.8 (ESXi)

 

Hope this helps. Let us know if this playbook was what you were looking for.

 

Regards
Luděk Matoušek
JNCIS-ENT, JNCIS-SP, JNCIP-SEC, JNCIA-DevOps