We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
Can I pull the hostname, IP, interface description from a web tool?
Hello. I'm trying to pass the configuration of several interfaces through a template and cannot get it to work.
My task is defined as:
- name: push config to IOS device
ios_config:
provider: "{{ nxos_provider }}"
authorize: yes
src: set_ports.j2
My jinja template is:
{% for item in ios_mods %}
{% set list = item.split(';') %}
"{{ list[0] }}"
{% for line in list[1:] %}
"{{ line }}"
{% endfor %}
{% endfor %}
And the data in ios_mods is:
"ios_mods": [
"interface TenGigabitEthernet1/0/1 ; description EXEMPT",
"interface GigabitEthernet1/0/49 ; shutdown ; switchport access vlan 999 ; description EXEMPT",
"interface FastEthernet0 ; shutdown ; switchport access vlan 999 ; description EXEMPT",
"interface Vlan1 ; shutdown ; description EXEMPT",
"interface Vlan2 ; shutdown ; switchport access vlan 999 ; description EXEMPT",
"interface TenGigabitEthernet1/0/2 ; description EXEMPT",
"interface Port-channel1 ; description EXEMPT",
"interface GigabitEthernet1/0/50 ; switchport access vlan 999 ; shutdown ; description EXEMPT"
]
The error I'm getting is:
fatal: [mgmt_sw]: FAILED! => {
"changed": false,
"command": "\"interface TenGigabitEthernet1/0/1 \"",
"failed": true,
"invocation": {
"module_args": {
"after": null,
"auth_pass": null,
"authorize": true,
"backup": false,
"before": null,
"config": null,
"defaults": false,
"force": false,
"host": null,
"lines": null,
"match": "line",
"multiline_delimiter": "@",
"parents": null,
"password": null,
"port": null,
"provider": {
"auth_pass": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"authorize": true,
"host": "mgmt_sw",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": null,
"ssh_keyfile": null,
"timeout": null,
"username": "admin"
},
"replace": "line",
"save": false,
"src": "\t\"interface TenGigabitEthernet1/0/1 \"\n\t\" description EXEMPT\"\n\t\t\"interface GigabitEthernet1/0/49 \"\n\t\" shutdown \"\n\t\" switchport access vlan 999 \"\n\t\" description EXEMPT\"\n\t\t\"interface FastEthernet0 \"\n\t\" shutdown \"\n\t\" switchport access vlan 999 \"\n\t\" description EXEMPT\"\n\t\t\"interface Vlan1 \"\n\t\" shutdown \"\n\t\" description EXEMPT\"\n\t\t\"interface Vlan2 \"\n\t\" shutdown \"\n\t\" switchport access vlan 999 \"\n\t\" description EXEMPT\"\n\t\t\"interface TenGigabitEthernet1/0/2 \"\n\t\" description EXEMPT\"\n\t\t\"interface Port-channel1 \"\n\t\" description EXEMPT\"\n\t\t\"interface GigabitEthernet1/0/50 \"\n\t\" switchport access vlan 999 \"\n\t\" shutdown \"\n\t\" description EXEMPT\"\n\t\n",
"ssh_keyfile": null,
"timeout": null,
"username": null
}
},
"msg": "Traceback (most recent call last):\n File \"/Users/patrickbrown/Library/Python/2.7/bin/ansible-connection\", line 198, in run\n (rc, stdout, stderr) = self.conn.exec_command(cmd)\n File \"/Users/patrickbrown/Library/Python/2.7/lib/python/site-packages/ansible/plugins/connection/network_cli.py\", line 248, in exec_command\n if obj['command'] == 'close_shell()':\nTypeError: string indices must be integers\n",
"rc": 255
I also tried updating it to include the multiline delimiter of "@" in the j2 template:
{% for item in ios_mods %}
{% set list = item.split(';') %}
"{{ list[0] }} @"
{% for line in list[1:] %}
"{{ line }} @"
{% endfor %}
{% endfor %}
but got the same results:
"src": "\t\"interface TenGigabitEthernet1/0/1 @\"\n\t\" description EXEMPT @\"\n\t\t\"interface TenGigabitEthernet1/0/2 @\"\n\t\" description EXEMPT @\"\n\t\t\"interface FastEthernet0 @\"\n\t\" shutdown @\"\n\t\" switchport access vlan 999 @\"\n\t\" description EXEMPT 2.5.3 @\"\n\t\t\"interface Vlan1 @\"\n\t\" shutdown @\"\n\t\" description EXEMPT @\"\n\t\t\"interface Vlan2 @\"\n\t\" shutdown @\"\n\t\" switchport access vlan 999 @\"\n\t\" description EXEMPT @\"\n\t\t\"interface GigabitEthernet1/0/49 @\"\n\t\" switchport access vlan 999 @\"\n\t\" shutdown @\"\n\t\" description EXEMPT @\"\n\t\t\"interface Port-channel1 @\"\n\t\" description EXEMPT @\"\n\t\t\"interface GigabitEthernet1/0/50 @\"\n\t\" switchport access vlan 999 @\"\n\t\" shutdown @\"\n\t\" description EXEMPT @\"\n\t\n",
Any help would be greatly appreciated!
Thank you, Patrick
Nevermind :)
I ended up changing my methodology. I created a filter to bust the lines in to individual statements that I could then iterate through with a with_items list.
- name: push config to IOS device
ios_config:
provider: "{{ nxos_provider }}"
authorize: yes
parents: "{{ item.int }}"
lines: "{{ item.lines }}"
with_items: "{{ ios_mods|b_filter }}"
filter:
def b_filter(self, b_variable):
temp_list = []
for temp_item in b_variable:
line_list = temp_item.split(';')
int = line_list[0]
int_items = ""
for items in line_list[1:]:
temp_list.append({'int': int, 'lines': items})
return temp_list
not quite as sexy, but works a lot better! ;)
Hi PATRICK : Excelente post! refer of theme, I'm searching how can I put the output from ansible in a web site or a graphic environment for monitoring and reporting.
Thanks,
Thank you for such good article.
I noticed that the example (TEST-ACL) you've provided to explain usage of "match and replace", has some issues,
First, the module should be "ios_config" instead of "ios_command". It also needs authorize parameter with ios_config.
I also noticed that the provided playbook will achieve the desired state even with "match: exact , replace: line"
Ah, yes I'd written ios_command where it should have been ios_config. Thank you for that! I've changed those instances. Authorize is only needed if your user is logged into user mode instead of privileged mode. If you remove the access-list before applying you need replace: block, otherwise the lines which were already there will be missing.
When I want to have the effect of
match: exact,replace: blockbut for general configurations that are not within a block, like the SNMP example in the article, you mentioned a workaround which I can differ against a pre-filtered file. Is there a way using this same workaround to remove undesired lines from the general configuration to have the same effect as as I would have withmatch: exact,replace: block?