2

I am trying to create a csv file with the following Ansible playbook:


  - name: Find Fex Enclosure
    hosts: MAQ
    gather_facts: no
    connection: local

    tasks:


      - name: GET VENDOR & OS OF THE EQUIPEMENT
        snmp_device_version:
          host={{ inventory_hostname }}
          version=3
          integrity=xxxx
          level=authPriv
          privacy=xxxx
          username=xxxxxx
          authkey=xxxxxxx
          privkey=xxxxxxx

      - name: SHOW FEX
        ntc_show_command:
          connection=netmiko_ssh
          platform={{ ansible_device_vendor }}_{{ ansible_device_os }}
          command='show fex'
          host={{ inventory_hostname }}
          username={{ ansible_user }}
          password={{ ansible_pass }}
          template_dir=/usr/share/ansible/plugins/modules/ntc-ansible/ntc-templates/templates/
        register: fex_list


      - name: SHOW FEX By ID
        ntc_show_command:
          connection=netmiko_ssh
          platform={{ ansible_device_vendor }}_{{ ansible_device_os }}
          command='show fex {{ item.number }}'
          host={{ inventory_hostname }}
          username={{ ansible_user }}
          password={{ ansible_pass }}
          template_dir=/usr/share/ansible/plugins/modules/ntc-ansible/ntc-templates/templates/
        register: fex_conf
        with_items: "{{ fex_list.response }}"

      - name: create File.csv with content from fex_conf
        copy: 
          content: "{{ inventory_hostname }};{{ item.1.fex }};{{ item.1.description }};{{ item.1.extender_serial }};{{ item.1.extender_model }};{{ item.1.enclosure }};{{ item.1.enclosure_serial }};{{ item.1.fabric_port }}\n"
          dest: out/file.csv
        loop: "{{ fex_conf.results | subelements('response') }}"
        when: item.1.enclosure !=""

The problem is that it only writes the last iteration to the file.csv

this is what I got:

cat out/file.csv 999.999.999.8;114;MEF114-999-SS1999;FOC199999;N2K-B22HP-P;SS1-999-12;CZ3....;Eth1/13

I am expecting at least 6 lines. Do not know what to do, when I do a debug I get the 6 lines as msg.SO the loop is working.

I have also tried the Template way, but stuck in doing a loop with subelements on Jinja2. I do not know how to do it.

If any one could point me in the right direcction, i will appreciated.

Many Thnaks

Jose Garcia
  • 21
  • 1
  • 4

1 Answers1

1

I think there are a couple of problems here. I am not sure that fex_conf contains what you think it does. The SHOW FEX By ID task is being run through a loop containing the items in fex_list.response. On each pass, it registers the variable fex_conf. More specifically, it overwrites the contents of fex_conf, on each pass.

Add this task to confirm that suspicion:

- debug:
    var: fex_conf

Then you use the copy command, also passing it a loop, which also suffers from the same problem. The copy command creates the file on the target on each pass, with the current contents of the loop as its contents. This is therefore overwritten on each pass.

One possible solution would be to split a couple of tasks out into a separate file process_fex.yml

---
# process_fex.yml

- name: SHOW FEX By ID
  ntc_show_command:
    connection=netmiko_ssh
    platform={{ ansible_device_vendor }}_{{ ansible_device_os }}
    command='show fex {{ fex_data.number }}'
    host={{ inventory_hostname }}
    username={{ ansible_user }}
    password={{ ansible_pass }}
    template_dir=/usr/share/ansible/plugins/modules/ntc-ansible/ntc-templates/templates/
  register: fex_conf

- name: update File.csv with content from fex_conf
  lineinfile:
    dest: out/file.csv
    line: "{{ inventory_hostname }};{{ item.1.fex }};{{ item.1.description }};{{ item.1.extender_serial }};{{ item.1.extender_model }};{{ item.1.enclosure }};{{ item.1.enclosure_serial }};{{ item.1.fabric_port }}"
    create: yes
  loop: "{{ fex_conf.results | subelements('response') }}"

Then, you can include this file and attach the output of SHOW FEX to it as a loop:

- name: SHOW FEX
  ntc_show_command:
    connection=netmiko_ssh
    platform={{ ansible_device_vendor }}_{{ ansible_device_os }}
    command='show fex'
    host={{ inventory_hostname }}
    username={{ ansible_user }}
    password={{ ansible_pass }}
    template_dir=/usr/share/ansible/plugins/modules/ntc-ansible/ntc-templates/templates/
  register: fex_list

- include: process_fex.yml
  loop: "{{ fex_list }}"
  loop_control:
    loop_var: fex_data

The loop_control.loop_var param, sets a custom name for the loop variable, which otherwise defaults to item. Failure to do this, can result in odd problems, when the included file itself contains a loop.

lineinfile simply adds a new line to a file, so passing it a loop is safe because it will not overwrite existing content. create: yes ensures that lineinfile will create a blank file if it does not exist on the first pass.

There maybe cleaner solutions that other will post, but hopefully this would be enough to get you moving.

clockworknet
  • 2,736
  • 1
  • 15
  • 19