0
votes

I'm receiving the below error when trying to deploy the below Ansible script. It is related to the copying of the yum output to a .txt file and does seem to be something trivial with the syntax. Any help decoding the error would be much appreciated.

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
1
'dict object' has no attribute 'stdout' => debug the content of yumoutput and you will see it does not contain any stdout key. Choose the correct key you want to display in your file, or the entire varialb all together, in yaml format for convenience => content: "{{ yumoutput | to_nice_yaml(indent=2) }}"Zeitounator
Thanks for this. It works! I suspect there must be other formats you can output eg. json, plain text etc.BigRed247
Thank you very much for your help. This proved to be very useful.BigRed247

1 Answers

1
votes

You got this error because the return of - name: Run yum update and output details task does not contain any attribute stdout. The error is quite self-explained. if you debug yumoutput you'll see a json and no stdout key, since all ansible modules must return a json when registering to a variable.

Take care of checking the json response structure of every module you're using to make sure if some key is missing before calling it.

The easiest and fastest way to do this is just showing the registered variable with debug module.

e.g.

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