【发布时间】:2021-06-25 13:18:46
【问题描述】:
我在尝试部署以下 Ansible 脚本时收到以下错误。它与将 yum 输出复制到 .txt 文件有关,并且在语法上似乎是微不足道的。任何帮助解码错误将不胜感激。
TASK [将输出复制到本地文件]****************************************** ****
致命:[Dev-01]:失败! => {"msg": "该任务包含一个带有未定义变量的选项。错误是:'dict object' has no attribute 'stdout'\n\n错误似乎在 '/tmp/awx_728_j8h4pd86/project/linux -patch-script-1.yml':第 26 行,第 5 列,但可能\n位于文件中的其他位置,具体取决于确切的语法问题。\n\n违规行似乎是:\n\n\n - 名称:将输出复制到本地文件\n ^ here\n"}**
致命:[Prod-01]:失败! => {"msg": "该任务包含一个带有未定义变量的选项。错误是:'dict object' has no attribute 'stdout'\n\n错误似乎在 '/tmp/awx_728_j8h4pd86/project/linux -patch-script-1.yml':第 26 行,第 5 列,但可能\n位于文件中的其他位置,具体取决于确切的语法问题。\n\n违规行似乎是:\n\n\n - 名称:将输出复制到本地文件\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
【问题讨论】:
-
'dict object' has no attribute 'stdout'=> 调试yumoutput的内容,你会发现它不包含任何stdout键。为方便起见,选择要在文件中显示的正确键,或整个 varialb,以 yaml 格式显示 =>content: "{{ yumoutput | to_nice_yaml(indent=2) }}" -
谢谢。有用!我怀疑必须有其他格式可以输出,例如。 json、纯文本等
-
非常感谢您的帮助。事实证明这非常有用。
标签: ansible yaml ansible-template