Hi everyone,
Apologies for what may be a very simple question, but I feel like I’m going in circles. I have a role that lists available updates in Linux. All I want is for that data to be returned to the playbook that calls the role. According to this post on Stackoverflow the variable should be accessible, but it isn’t because it is registered within a block. Here’s my playbooks:
# Playbook that calls the role
---
- name: Update machines using the default configuration
hosts: linux
become: true
roles:
- role: ayvens.update_roles.list_linux_updates
# generates update metadata in list_linux_updates_updatable_packages
tasks:
- name: List available package updates (From playbook)
ansible.builtin.debug:
var: list_linux_updates_updatable_packages.stdout
# Role
---
- name: List updates
block:
- name: Updates for RedHat/SUSE and derived distributions
when: ansible_facts.pkg_mgr == 'dnf'
block:
- name: Gather available package updates for RedHat/SUSE systems
ansible.builtin.command:
cmd: dnf check-update --quiet
register: list_linux_updates_updatable_packages
changed_when: list_linux_updates_updatable_packages.rc in [0, 100]
failed_when: list_linux_updates_updatable_packages.rc not in [0, 100]
- name: List available package updates (Inside dnf block)
ansible.builtin.debug:
var: list_linux_updates_updatable_packages.stdout
- name: Updates for Debian/Ubuntu and derived distributions
when: ansible_facts.pkg_mgr == 'apt'
block:
- name: Gather available package updates for Debian/Ubuntu systems
ansible.builtin.command:
cmd: apt-get upgrade --dry-run
register: list_linux_updates_updatable_packages
changed_when: list_linux_updates_updatable_packages.rc == 0
- name: List available package updates (From role)
ansible.builtin.debug:
var: list_linux_updates_updatable_packages.stdout
As you can see, I have done some debugging with three prints. Only the one from inside the dnf block works. From what I’ve read and observed, this variable is restricted to the scope of the block in which it is defined. But I need it on a much higher scope. So, I need to either:
- Parse the variable “up the chain” somehow, or
- Register the variable on a higher scope (but sadly register doesn’t work on a block, which makes sense)
I’ve looked for methods to change the scope of variables, take their values outside of blocks, alternative methods of defining them, but I was unable to find what I need. Any input is much appreciated!