0

The below code is being used to append to a config file. What I want to do is introduce a loop loop: "{{ INTERFACE }}", but this does not work (as the temp file will be overwritten), template doesn't have an append option.

Any suggestions on how to fix?

- name: Build CE config Cisco
  template:
    src=/opt/netsec/ansible/orchestration/cisco_templates/NOKIA_T1_PORT.js
    dest=/opt/netsec/ansible/orchestration/config_outputs/tmp/{{CE_HOSTNAME}}
   loop: "{{ INTERFACE }}"

- name: Build PE config Cisco output
  shell: cat /opt/netsec/ansible/orchestration/config_outputs/tmp/{{CE_HOSTNAME}} >> /opt/netsec/ansible/orchestration/config_outputs/new/{{CE_HOSTNAME}}.conf

INTERFACES looks like this:

INTERFACES:
  - INTERFACE: Gi0/3/0
    EQUIPMENT_DESCRIPTION: ETHXXXXSR01
  - INTERFACE: Gi0/3/1
    EQUIPMENT_DESCRIPTION: ETHXXXXSR01_STANDBY
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Jonathan E
  • 54
  • 2
  • 12
  • 1
    What you have in `{{ INTERFACE }}` is not clear. However you should probably loop on `INTERFACE` inside the template file. – seshadri_c Sep 03 '20 at 13:56
  • Does this answer your question? [how to iterate over a list of list in jinja](https://stackoverflow.com/questions/30056622/how-to-iterate-over-a-list-of-list-in-jinja) – toydarian Sep 03 '20 at 14:05
  • Thanks for your answer, I don't think I can do this because the file already exists with content that is not being added in by this section. – Jonathan E Sep 03 '20 at 14:10
  • Does this answer your question? [Appending files with Template Module in Ansible](https://stackoverflow.com/questions/46366526/appending-files-with-template-module-in-ansible) – β.εηοιτ.βε Sep 03 '20 at 15:07

2 Answers2

2

If the template is multi-line

shell> cat nokia_t1_port.j2
Equipment {{ item.EQUIPMENT_DESCRIPTION }}
  interface {{ item.INTERFACE }}

use blockinfile. For example

    - blockinfile:
        insertafter: EOF
        path: ce_hostname.conf
        block: "{{ lookup('template', 'nokia_t1_port.j2') }}"
        marker: ""
      loop: "{{ INTERFACES }}"

gives

shell> cat ce_hostname.conf

Equipment ETHXXXXSR01_STANDBY
  interface Gi0/3/1

Equipment ETHXXXXSR01
  interface Gi0/3/0

Because of the "marker", there will an empty line among the appended blocks.


If the template is single-line

shell> cat nokia_t1_port.j2
{{ item.EQUIPMENT_DESCRIPTION }} interface {{ item.INTERFACE }}

use lineinfile. For example

    - lineinfile:
        insertafter: EOF
        path: ce_hostname.conf
        line: "{{ lookup('template', 'nokia_t1_port.j2') }}"
      loop: "{{ INTERFACES }}"

gives

shell> cat ce_hostname.conf
ETHXXXXSR01 interface Gi0/3/0
ETHXXXXSR01_STANDBY interface Gi0/3/1
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
1

From the description, it sounds like you would like to use the existing contents of NOKIA_T1_PORT.js file, but add some content using INTERFACES at the destination.

However you can not do this with one template task. You could do something like this:

- name: Build CE config Cisco
  copy:
    src: /opt/netsec/ansible/orchestration/cisco_templates/NOKIA_T1_PORT.js
    dest: "/opt/netsec/ansible/orchestration/config_outputs/tmp/{{ CE_HOSTNAME }}"

- name: Update interface info
  blockinfile:
    path: "/opt/netsec/ansible/orchestration/config_outputs/tmp/{{ CE_HOSTNAME }}"
    block: |
      {% for item in INTERFACES %}
      INTERFACE: {{ item.INTERFACE }}
      EQUIPMENT_DESCRIPTION: {{ item.EQUIPMENT_DESCRIPTION }}
      {% endfor %}
seshadri_c
  • 6,906
  • 2
  • 10
  • 24