Skip to content

Commit

Permalink
Fix(eos_cli_config_gen): Support vars on play via vars or `vars_fil…
Browse files Browse the repository at this point in the history
…es` (#2999)
  • Loading branch information
ClausHolbechArista authored Jul 24, 2023
1 parent 1fd31c0 commit bfa7f3e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
hosts: test-hosts
gather_facts: false
connection: local
vars:
tacacs_key_set_as_play_var: "071B245F5A"
tasks:

- name: Generate device intended config and documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ tacacs_servers:
single_connection: false
- host: 10.10.10.158
vrf: default
key: 071B245F5A
# Testing of play_vars in eos_cli_config_gen
key: "{{ tacacs_key_set_as_play_var }}"
key_type: 7
- host: 10.10.10.159
vrf: default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
hosts: all
gather_facts: false
connection: local
vars:
string_set_as_play_var: "test of var set under play vars"
tasks:

- name: Generate intended variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ interface Ethernet8
description PHYSICAL_PORT_DESCRIPTION
no shutdown
channel-group 7 mode active
!
interface Ethernet9
description test of var set under play vars
no shutdown
switchport
no ip routing vrf MGMT
!
management api http-commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ ethernet_interfaces:
channel_group:
id: 7
mode: active
- name: Ethernet9
peer: SERVER_WITH_PLAYVAR_DESCRIPTION
peer_type: server
description: test of var set under play vars
shutdown: false
type: switched
port_channel_interfaces:
- name: Port-Channel5
description: OLD_SW-1/5_PORT_CHANNEL_DESCRIPTION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ servers:
port_channel:
mode: "active"
description: "PORT_CHANNEL_DESCRIPTION"
# Testing of play_vars in eos_designs_structured_config
# Single port override physical port description with play_var
- name: SERVER_WITH_PLAYVAR_DESCRIPTION
adapters:
- switches: [connected_endpoints]
switch_ports: [Ethernet9]
description: "{{ string_set_as_play_var }}"
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,21 @@ def run(self, tmp=None, task_vars=None):

# Build data from hostvars and role default vars
hostname = task_vars["inventory_hostname"]
self.data = self._templar.template(self._task._role.get_default_vars())
self.data.update(task_vars["hostvars"].get(hostname))

# Read ansible variables and perform templating to support inline jinja2
for var in task_vars:
if str(var).startswith(("ansible", "molecule", "hostvars", "vars", "avd_switch_facts")):
continue
if self._templar.is_template(task_vars[var]):
# Var contains a jinja2 template.
try:
task_vars[var] = self._templar.template(task_vars[var], fail_on_undefined=False)
except Exception as e:
raise AnsibleActionFail(f"Exception during templating of task_var '{var}'") from e

if not isinstance(task_vars, dict):
# Corner case for ansible-test where the passed task_vars is a nested chain-map
task_vars = dict(task_vars)

# Load schema tools and perform conversion and validation
avdschematools = AvdSchemaTools(
Expand All @@ -55,7 +68,7 @@ def run(self, tmp=None, task_vars=None):
validation_mode=validation_mode,
plugin_name=task_vars["ansible_role_name"],
)
result.update(avdschematools.convert_and_validate_data(self.data))
result.update(avdschematools.convert_and_validate_data(task_vars))

# Template to file
# Update result from Ansible "copy" operation (setting 'changed' flag accordingly)
Expand All @@ -68,7 +81,7 @@ def template(self, task_vars, dest):
# Get updated templar instance to be passed along to our simplified "templater"
templar = get_templar(self, task_vars)

output = template(self.templatefile, self.data, templar)
output = template(self.templatefile, task_vars, templar)
if self.add_md_toc:
output = add_md_toc(output, skip_lines=self.md_toc_skip_lines)

Expand Down

0 comments on commit bfa7f3e

Please sign in to comment.