使用 fo 循环进行寄存器输出的 Ansible Jinja2 模板问题

使用 fo 循环进行寄存器输出的 Ansible Jinja2 模板问题

我正在使用 ansible 获取少数服务的状态,并使用 ansible jinja 模板生成 HTML 输出,我收到变量未定义或其他一些错误,

在这里,我将值存储在寄存器模块中,然后在模板中获取这些值,但它不起作用

金贾模板:

{% for network_switch in ['client'] %}
        <tr>
            <td>{{ hostvars[network_switch]['ansible_hostname'] }}</td>
            <td>{{ hostvars[network_switch]['kernel.stdout'] }}</td>
            <td>{{ hostvars[network_switch]['httpd.stdout'] }}</td>
        </tr>
{% endfor %}

剧本详细信息:

- name: Getting the OS Information
      command: 'uname -r'
      register: kernel

    - name: Getting the OS Information
      shell: "systemctl status sshd | grep -i active | awk '{print$3}'"
      register: httpd

    - name: create HTML report
      template:
        src: report.j2
        dest: "{{ file_path }}"
      delegate_to: localhost
      run_once: true

错误:

失败的! => {"changed": false, "msg": "AnsibleUndefinedVariable: \"hostvars['client']\" 未定义"}

答案1

使用ansible_play_batch “当前游戏中的活动主机列表受序列号(又称“批次”)限制。失败/无法访问的主机不被视为“活动”。”

{% for network_switch in ansible_play_batch %}
...

如果有更多批次,文件将被覆盖。看控制剧本执行:策略等。看Ansible 日期变量如何使用日期并创建一个唯一的文件名。例如

dest: "{{ file_path ~ '-' ~
          ansible_date_time.date ~ '-' ~
          ansible_date_time.hour ~ '-' ~
          ansible_date_time.minute ~ '-' ~
          ansible_date_time.second }}"

创建单个文件

下一个选项是创建一个包含第一个播放中的所有项目的列表,并在第二个播放中写入文件(更改模板并使用我的列表)。例如

- hosts: all
  tasks:
    - name: Getting the OS Information
      ...
    - name: Collect the list
      set_fact:
        my_list: "{{ my_list|default([]) +
                     [hostvars[item]['ansible_hostname'],
                      hostvars[item]['kernel.stdout'],
                      hostvars[item]['httpd.stdout']] }}"
       loop: "{{ ansible_play_batch }}"
       run_once: true

- hosts: all
  tasks:
    - name: Take a look at what was collected
      debug:
        var: my_list
      run_once: true
    - name: create HTML report
      template:
        src: report.j2
        dest: "{{ file_path }}"
      delegate_to: localhost
      run_once: true

(未测试)

答案2

剧本:

---
- name: build Centos inventory report
  hosts: client

  vars:
    file_path: /var/www/html/generated_report.html

  tasks:
    #- name: Getting the OS Information
    #  command: 'cat /etc/redhat-release'
    #  register: os_release

    - name: Getting the OS Information
      command: 'uname -r'
      register: kernel

    - name: Getting the OS Information
      shell: "systemctl status sshd | grep -i active | awk '{print$3}'"
      register: httpd

    - name: Collect the list
      set_fact:
        my_list: "{{ my_list|default([]) +
                     [hostvars[item]['ansible_hostname'],
                      hostvars[item]['kernel.stdout'],
                      hostvars[item]['httpd.stdout']] }}"
      loop: "{{ ansible_play_batch }}"
      run_once: true

- name: Collecting all information
  hosts: client
  vars:
    file_path: /var/www/html/generated_report.html
  tasks:
    - name: Take a look at what was collected
      debug:
        var: my_list
      run_once: true

    - name: create HTML report
      template:
        src: report.j2
        dest: "{{ file_path }}"
      delegate_to: localhost
      run_once: true

金贾模板:

{% for network_switch in ansible_play_batch %}
        <tr>
            <td>{{ hostvars[network_switch]['ansible_hostname'] }}</td>
            <td>{{ hostvars[network_switch]['kernel.stdout'] }}</td>
            <td>{{ hostvars[network_switch]['httpd.stdout'] }}</td>
        </tr>
{% endfor %}

相关内容