cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
6146
Views
20
Helpful
3
Replies

config SNMP using Ansible

Saskiaaa
Level 1
Level 1

Hallo,

 

im trying to create a playbook for SNMP Configurations and i need your help/suggestions.

 

so i want to config this 4 commands for example in one run,

playbook:

  - ios_config:

       lines:     

  - " snmp-server group {{ group_name }} {{ snmp_version }}  {{ security}}"

vars file:

---

# vars file for snmp_config

group_name: "group_1

version: "V3"

security_level: "priv"

 

 

this work for this command: "smp-server group group_1 v3 priv  "

but i dont know how to edit the line to be dynamic for all other options 

 

i hope you understand my question.

 

Regards,

 

 

 

 

 

 

1 Accepted Solution

Accepted Solutions

Seb Rupik
VIP Alumni
VIP Alumni

Hello again,

How about using a dictionary instead of list:

 

---

- name: get_vars
  include_vars:
    file: snmp_options.yaml

- name: the_snmp_bit
  ios_config:
    lines:     
      - " snmp-server group {{ item.type }} {{ item.options }}"
  loop:
    - { type: "READ-ONLY", options: "v3 auth context vlan- match prefix" }
    - { type: "READ-WRITE", options: "v3 priv read SNMPVIEW write SNMPVIEW" }

...

 

 

If your goal is to reduce the number of commands in the playbook then you will need to hide the logic for building the SNMP parameters elsewhere. Just embedding the constructed strings in another var file will work. The more dynamic you try to make the SNMP parameter string building logic the more lines of code you will eventually end up with!

 

cheers,

Seb.

View solution in original post

3 Replies 3

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

Why not create a loop which iterates over a list containing the different SNMP option permutations. Something like:

---

- name: the_snmp_bit ios_config: lines: - " snmp-server group {{ group_name }} {{ version }} {{ security_level }} {{ item }}" loop:
- "" - "context -vlan match prefix"
- "read SNMPVIEW write SNMPVIEW"
- "context -vlan match prefix"
...

...but then you are just hardcoding the variables into the playbook which is a bad thing. So you could create a new var file and call that:

---

- name: get_vars
  include_vars:
    file: snmp_options.yaml

- name: the_snmp_bit
  ios_config:
    lines:     
      - " snmp-server group {{ group_name }} {{ version }}  {{ security_level }} {{ item }}"
  loop:
    - "{{ snmp_options }}"

...

With the var snmp_options.yaml file looking like this:

 

---
- snmp_options:
- ""
- "context -vlan match prefix"
- "read SNMPVIEW write SNMPVIEW"
- "context -vlan match prefix"

...

cheers,

Seb.

Hi Seb,

 

first thanks for your replay,

HINT: im using ansible roles 

your solution will be good if i have only one group , but in my case i have two diff groups ( group_1 , group_2 ) 

 

 is there any other solution to prevent writing the whole command ?

 

thanks 

 

Seb Rupik
VIP Alumni
VIP Alumni

Hello again,

How about using a dictionary instead of list:

 

---

- name: get_vars
  include_vars:
    file: snmp_options.yaml

- name: the_snmp_bit
  ios_config:
    lines:     
      - " snmp-server group {{ item.type }} {{ item.options }}"
  loop:
    - { type: "READ-ONLY", options: "v3 auth context vlan- match prefix" }
    - { type: "READ-WRITE", options: "v3 priv read SNMPVIEW write SNMPVIEW" }

...

 

 

If your goal is to reduce the number of commands in the playbook then you will need to hide the logic for building the SNMP parameters elsewhere. Just embedding the constructed strings in another var file will work. The more dynamic you try to make the SNMP parameter string building logic the more lines of code you will eventually end up with!

 

cheers,

Seb.