尝试使用Ansbile复制模块,内容选项创建.csv文件,但仅写入循环的最后一次迭代

时间:2019-01-22 10:15:38

标签: loops ansible ansible-template

我正在尝试使用以下Ansible剧本创建一个csv文件:


  - 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 !=""

问题在于它仅将最后一次迭代写入文件。csv

这就是我得到的:

  

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

我希望至少有6行。 不知道该怎么办,当我进行调试时,我得到了6行作为msg.SO,循环正在起作用。

我也尝试了Template方法,但是在Jinja2上使用子元素进行循环。我不知道该怎么做。

如果有人能指出我正确的方向,我将不胜感激。

很多人

1 个答案:

答案 0 :(得分:1)

我认为这里有两个问题。我不确定fex_conf是否包含您认为的内容。 SHOW FEX By ID任务正在循环执行,循环包含fex_list.response中的项目。每次通过时,它都会注册变量fex_conf。更具体地说,每次遍历都会覆盖fex_conf的内容。

添加此任务以确认可疑之处:

- debug:
    var: fex_conf

然后,您使用copy命令,还向其传递一个循环,该循环也遭受相同的问题。 copy命令每次通过都会在目标文件上创建文件,并将循环的当前内容作为其内容。因此,每次通过时都会覆盖。

一种可能的解决方案是将几个任务分解为一个单独的文件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') }}"

然后,您可以包含此文件并将SHOW FEX的输出作为循环附加到该文件:

- 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

loop_control.loop_var参数为循环变量设置一个自定义名称,否则默认为item。否则,当包含的文件本身包含循环时,可能会导致奇怪的问题。

lineinfile只是在文件中添加了新行,因此将其传递给循环是安全的,因为它不会覆盖现有内容。 create: yes确保lineinfile会在第一次通过时不创建空白文件。

也许其他人会发布更清洁的解决方案,但希望这足以让您动起来。