1

I am using ansible [core 2.11.10] and I recently made the following yaml file :

- name: Linux Security Patching Playbook
  hosts: Linux_NPROD
  become: true
  become_user: root
  vars:
    ansible_python_interpreter: auto_silent
  tasks:
    - name : " Debian / Ubuntu Patching "
      shell : 'grep security /etc/apt/sources.list > /tmp/security.list && sudo apt-get upgrade -oDir::Etc::Sourcelist=/tmp/security.list -s'
      register: response
      when : ansible_os_family == "Debian" or ansible_os_family == "Ubuntu"
    - debug: msg="{{ response.stdout }}"

    - name: " CentOS / RHEL Patching "
      shell : 'yum update --security'
      register: x
      when : ansible_os_family == "RedHat"
    - debug: msg="{{ x.stdout }}"

The output works fine with the "Debian / Ubuntu Patching" task but on the second task I got the following error :

fatal: []: 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 '/home/superuser/Ansible/playbooks/security_patching.yaml': line 18, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n when : ansible_os_family == "RedHat"\n - debug: msg="{{ x.stdout }}"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n"}

The same error occurs when I modify the yaml file like this :

- name: Linux Security Patching Playbook
  hosts: Linux_NPROD
  gather_facts: true
  become: true
  become_user: root
  vars:
    ansible_python_interpreter: auto_silent
  tasks:
    - name : " Debian / Ubuntu Patching "
      shell : 'grep security /etc/apt/sources.list > /tmp/security.list && sudo apt-get upgrade -oDir::Etc::Sourcelist=/tmp/security.list -s'
      register: response
      when : ansible_os_family == "Debian" or ansible_os_family == "Ubuntu"
    - debug: msg="{{ response.stdout }}"

    - name: " CentOS Patching "
      yum:
       security: yes
       state: latest
      check_mode: yes
      register: yum_output
      become: true
      when : ansible_os_family == "RedHat"
    - debug: msg="{{ yum_output.stdout }}"

on the last yaml file if I comment on the first task then the second task works fine.

Any ideas of how I could solve this error?

2
  • I'm assuming the first task fails if the when: condition for that task isn't fulfilled, right? When you run against a Redhat machine, for example?
    – Kusalananda
    Jun 10, 2022 at 16:23
  • 1
    Kusalananda is probably right - if you only need to do that check sometimes, use when: response is defined
    – Panki
    Jun 10, 2022 at 16:51

1 Answer 1

1

A task like

- name: "CentOS / RHEL command"
  shell:
    cmd: 'echo "I was running"' 
  register: result
  when: ansible_os_family == "RedHat"

is executed or skiped based on facts. Therefore registering of a result will only happen if the condition was true.

Any ideas of how I could solve this error?

To not let fail the next task which will rely on the existence of a registered variable one could address this differently.

By adding the same condition as the task before

- name: Show result
  debug: 
    msg: "{{ result.stdout }}"
  when: ansible_os_family == "RedHat"

By adding a condition based on variables

  when: result.stdout is defined

By adding default values

- name: Show result
  debug: 
    msg: "{{ result.stdout | default('I was not running') }}"

By grouping tasks with blocks and executing them together if a condition is true.

If applicable, by addressing check_mode

- name: Show result
  debug: 
    msg: "{{ result.stdout | default('I was not running') }}"
  when: not ansible_check_mode

since the former task probably wasn't running if check_mode: false wasn't set and therefore also no result was registered.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .