Ansible:gather_subset - 无法获取名称中带有下划线的任何事实的信息

问题描述 投票:0回答:1

尝试生成显示

mount.device
mount.mount
mount.size_total
信息的报告。只要我包含任何包含下划线的内容,Playbook 就不会出现错误。例如,如果我删除
size_total
- 就可以了。添加
size_total
inode_total
block_size
会出现以下错误。

failed | msg: The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'size_total'

在没有

size_total
的情况下运行剧本 - 剧本有效。使用
size_total
运行剧本 - 剧本失败。使用
block_used
运行剧本 - 失败。

我对

size_total
的期望是:

localhost,/dev/mapper/sda1,/boot,533377024

我得到了什么

failed | msg: The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'size_total'
---

  - name: make a csv file
    hosts: g3prdapp
    gather_facts: false

    tasks:

      - ansible.builtin.setup:
          gather_subset:
            - all_ipv4_addresses
            - mounts
            - user_id

      - debug:
          var: ansible_mounts

      - debug:
          var: ansible_all_ipv4_addresses

      - debug:
          var: ansible_user_id

      - copy:
          dest: mount_output.csv
          content: |
            hostname,filesystem,mountpoint,size_total
            {% for hostname in ansible_play_hosts %}
            {% for mount in hostvars[hostname]['ansible_mounts'] %}
            {{ hostname }},{{ mount.device }},{{ mount.mount }},{{ mount.size_total }}
            {% endfor %}
            {% endfor %}
#        run_once: true
        delegate_to: localhost
ansible ansible-facts
1个回答
0
投票

一个最小的示例手册

---
- hosts: localhost
  become: false
  gather_facts: true
  gather_subset:
    - "!all"
    - "!min"
    - "mounts"

  tasks:

  - debug:
      msg: |
        hostname,filesystem,mountpoint,size_total
        {% for hostname in ansible_play_hosts %}
        {% for mount in hostvars[hostname]['ansible_mounts'] %}
        {{ hostname }},{{ mount.device }},{{ mount.mount }},{{ mount.size_total }}
        {% endfor %}
        {% endfor %}

结果为

的输出
TASK [debug] *****************************************************
ok: [localhost] =>
  msg: |-
    hostname,filesystem,mountpoint,size_total
    localhost,/dev/mapper/sysvg-lv_root,/,1234567890
    localhost,/dev/sda2,/boot,1234567890
    localhost,/dev/sda1,/boot/efi,1234567890
    localhost,/dev/mapper/sysvg-lv_home,/home,1234567890
    localhost,/dev/mapper/sysvg-lv_opt,/opt,1234567890
    localhost,/dev/mapper/sysvg-lv_var,/var,1234567890
    localhost,/dev/mapper/sysvg-lv_log,/var/log,1234567890
    localhost,/dev/mapper/sysvg-lv_audit,/var/log/audit,1234567890
    localhost,/dev/mapper/sysvg-lv_tmp,/tmp,1234567890

适用于 RHEL 8.9、RHEL 9.3 以及本地和远程受管节点。

根据当前提供的信息,您的问题无法重现。

© www.soinside.com 2019 - 2024. All rights reserved.