linux - Jinja 模板中的 Ansible 多个事实

标签 linux ansible jinja2

我想从主机中提取所有接口(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/

相关文章:

loops - 如何在 Ansible 中循环通过通配符标识的主机?

json - 使用 Ansible 从 JSON 响应中提取字段

jinja2 - SaltStack 中 Jinja 模板的自省(introspection)

python - 在共享 Web 服务器上安装 Flask 时只读文件系统。任何解决方法?

Linux BlueZ dbus 通信 a2dp

Ansible 安装 Debian 软件包

python - 如何格式化插入 Jinja2 模板中的特殊字符?

linux - 如何在启动进程后 10 秒内在 bash 中发送到标准输入?

linux - 如何保存两个变量的变化?

javascript - 段落中的嵌套跨度不会在子节点上换行,只会在父节点上换行