Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible : APT module: how to get stdout?

Using the following answer stackoverflow.com/questions/34026875/ I'm trying to get the stdout from ansible apt module.

Using this very simple test playbook:

- hosts: localhost
  sudo: true
  tasks:
    - name: 'apt: update & upgrade'
      apt:
        update_cache: yes
        cache_valid_time: 3600
      register: apt
    - debug: msg={{ apt.stdout.split('\n')[:-1] }}

I always have this error message:

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'

I have tried with many way (install package, remove, ... ) and always have the same error message.

Using debug: msg=apt the only output message that I can have is {"cache_update_time": 1531941358, "cache_updated": true, "changed": true} even if I install or remove a package which is for me in my humble opinion an output message which means nothing clear, because "cache_updated": true, "changed": true doe not mean that packages have been installed or removed.

In the Ansible APT module documentation Ansible APT Module Documentation Return Values section, there is a stdout returned success, when needed.

Does anyone know how to get a real and clear exit from the Ansible APT module or how to force Ansible to return a stdout (ansible config, ...) and what really means "success, * when needed *" because i never managed to get a stdout output under any circumstances.

Kind Regards

like image 333
moocan Avatar asked Mar 27 '26 07:03

moocan


1 Answers

Since the documentation ("when needed") is not very clear here, have a look at these lines in the source of the apt ansible module.

My understanding: The output of apt update is completely discarded unless the command fails, in which case it ends up in the error message.

I.e. you are best off with Michael Ababio's answer.

like image 70
leopold.talirz Avatar answered Apr 02 '26 22:04

leopold.talirz