Loops 尝试使用Anbile复制模块、内容选项创建.csv文件,但它只写入循环的最后一次迭代
我正在尝试使用以下Ansible playbook创建csv文件:Loops 尝试使用Anbile复制模块、内容选项创建.csv文件,但它只写入循环的最后一次迭代,loops,ansible,ansible-template,Loops,Ansible,Ansible Template,我正在尝试使用以下Ansible playbook创建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_hostn
- 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;1999年;N2K-B22HP-P;SS1-999-12;CZ3。。。。;Eth1/13
我期待至少6行。
不知道该怎么做,当我进行调试时,我得到了6行消息。因此循环正在工作
我也尝试过模板的方式,但在Jinja2上使用子元素进行循环。我不知道怎么做
如果有人能给我指出正确的方向,我将不胜感激
许多问题我认为这里有几个问题。我不确定fex_conf是否包含您认为的功能。
SHOW FEX By ID
任务正在通过一个循环运行,该循环包含FEX_列表中的项目。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\u control.loop\u var
参数为循环变量设置自定义名称,否则默认为项
。如果不这样做,当包含的文件本身包含循环时,可能会导致奇怪的问题
lineinfle
只需在文件中添加新行,因此传递循环是安全的,因为它不会覆盖现有内容<代码>创建:是确保lineinfle在第一次传递时不存在空白文件时创建该文件
其他人可能会发布更干净的解决方案,但希望这足以让你行动起来