03-30-2021 01:21 PM - edited 04-01-2021 06:16 AM
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,
Solved! Go to Solution.
04-01-2021 05:32 AM
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.
03-31-2021 03:53 PM
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.
04-01-2021 03:06 AM - edited 04-01-2021 06:18 AM
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
04-01-2021 05:32 AM
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.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide