我想从主机中提取所有接口(interface)名称,然后打印该接口(interface)的所有信息。
--- # Fetches network interfaces with IPs
- hosts: hta
gather_facts: yes
become: yes
tasks
- debug: msg=" {{ ansible_interfaces|length }}"
register: num
- name: moving template over to server
template: src=templates/network.j2 dest=/root/network_info.txt
和network.j2文件
{% for int in ansible_interfaces %}
Interfaces: Interface-{{ int }}
Data: ansible_{{ int }}
{% endfor %}
到目前为止,我无法打印信息,Ansible 将我的输入 ansible_{{ int }} 作为文字。
最佳答案
下图
- command: "ifconfig {{ item }}"
register: result
loop: "{{ ansible_interfaces }}"
- template:
src: template.j2
dest: int.txt
delegate_to: localhost
用这个模板
{% for int in result.results %}
Interfaces: Interface-{{ int.item }}
Data: {{ int.stdout }}
{% endfor %}
在 localhost 创建包含接口(interface)数据的文件 int.txt。
关于linux - Jinja 模板中的 Ansible 多个事实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55165106/