Meraki scenario guide - describe how to merge new data with old data (#48999)

* Described how to merge new data with old data in the Meraki guide
Co-Authored-By: kbreit <kevin.breit@kevinbreit.net>
This commit is contained in:
Kevin Breit 2018-11-29 15:42:10 -06:00 committed by Alicia Cozine
parent 02baae6e99
commit a9d68f3d52

View file

@ -124,6 +124,67 @@ Handling Returned Data
Since Meraki's response data uses lists instead of properly keyed dictionaries for responses, certain strategies should be used when querying data for particular information. For many situations, use the ``selectattr()`` Jinja2 function. Since Meraki's response data uses lists instead of properly keyed dictionaries for responses, certain strategies should be used when querying data for particular information. For many situations, use the ``selectattr()`` Jinja2 function.
Merging Existing and New Data
=============================
Ansible's Meraki modules do not allow for manipulating data. For example, you may need to insert a rule in the middle of a firewall ruleset. Ansible and the Meraki modules lack a way to directly merge to manipulate data. However, a playlist can use a few tasks to split the list where you need to insert a rule and then merge them together again with the new rule added. The steps involved are as follows:
1. Create blank "front" and "back" lists.
::
vars:
- front_rules: []
- back_rules: []
2. Get existing firewall rules from Meraki and create a new variable.
::
- name: Get firewall rules
meraki_mx_l3_firewall:
auth_key: abc123
org_name: YourOrg
net_name: YourNet
state: query
delegate_to: localhost
register: rules
- set_fact:
original_ruleset: '{{rules.data}}'
3. Write the new rule. The new rule needs to be in a list so it can be merged with other lists in an upcoming step. The blank `-` puts the rule in a list so it can be merged.
::
- set_fact:
new_rule:
-
- comment: Block traffic to server
src_cidr: 192.0.1.0/24
src_port: any
dst_cidr: 192.0.1.2/32
dst_port: any
protocol: any
policy: deny
4. Split the rules into two lists. This assumes the existing ruleset is 2 rules long.
::
- set_fact:
front_rules: '{{front_rules + [ original_ruleset[:1] ]}}'
- set_fact:
back_rules: '{{back_rules + [ original_ruleset[1:] ]}}'
5. Merge rules with the new rule in the middle.
::
- set_fact:
new_ruleset: '{{front_rules + new_rule + back_rules}}'
6. Upload new ruleset to Meraki.
::
- name: Set two firewall rules
meraki_mx_l3_firewall:
auth_key: abc123
org_name: YourOrg
net_name: YourNet
state: present
rules: '{{ new_ruleset }}'
delegate_to: localhost
Error Handling Error Handling
============== ==============