尝试输出到文件时出现 Ansible stdout 脚本错误

Ansible stdout script error when trying to output to file

我在尝试部署以下 Ansible 脚本时收到以下错误。它与将 yum 输出复制到 .txt 文件有关,并且在语法上似乎是微不足道的。任何帮助解码错误将不胜感激。

TASK [copy the output to a local file]*****************************************

fatal: [Dev-01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/tmp/awx_728_j8h4pd86/project/linux-patch-script-1.yml': line 26, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: copy the output to a local file\n ^ here\n"}**

fatal: [Prod-01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/tmp/awx_728_j8h4pd86/project/linux-patch-script-1.yml': line 26, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: copy the output to a local file\n ^ here\n"}****

---
- hosts: all
  become: yes
  tasks:
  - name: yum-clean-metadata
    command: yum clean metadata
    args:
      warn: no
    
  - name: Old CF output file for same of handover
    shell: rpm -qa --queryformat "%{NAME};%{VERSION}-%{RELEASE}\n" | sort -t\; -k 1 > /tmp/yum-Installed-pre.txt
    
  - name: Set variable to number of installed packages and available updates
    shell: "{{ item }}"
    with_items: 
      - export pre_pkg_inst=$(yum list installed | grep '^[a-Z0-9]' | wc -l)
      - export pre_pkg_avail=$(yum check-update --quiet | grep '^[a-Z0-9]' | wc -l)
      - echo -n "${HOSTNAME};${pre_pkg_inst};${pre_pkg_avail};" > /tmp/$HOSTNAME-yum-install.txt
    
  - name: Run yum update and output details
    yum:
      name: '*'
      state: latest
    register: yumoutput
    
  - name: copy the output to a local file
    copy:
      content: "{{ yumoutput.stdout }}"
      dest: "/tmp/yum-update.txt"
    
  - name: Reboot machine after update
    reboot:
      msg: Reboot initiated by Ansible after patching
      post_reboot_delay: 30
      reboot_timeout: 600

您收到此错误是因为 - name: Run yum update and output details 任务的 return 不包含任何属性标准输出。该错误是不言自明的。如果你调试 yumoutput 你会看到一个 json 而没有 stdout 键,因为所有 ansible 模块在注册到变量时必须 return 一个 json。

注意检查您正在使用的每个模块的 json 响应结构,以确保在调用它之前是否缺少某些键。

最简单和最快的方法是只显示带有 debug 模块的注册变量。

例如

- name: Show registered var contents and structure
  debug:
    var: yumoutput