2
votes

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

3
@techraf the ansible apt module seems to get the aptitude binary ` APTITUDE_CMD = module.get_bin_path("aptitude", False)` but i'm not a python expert !moocan
@techraf, do you mean that for method as aptitude update, install, purge python-apt is used .. so no output or poor one and for method as safe-upgrade or dist-upgrade there is an output because aptitude or apt-get is used ?moocan
Frankly speaking, I don't know. It looks more or less so, some other modules also worked that way, but you'd need to trace exactly how this module and libraries work. And then you'd arrive only at the explanation why. I see no problem to solve here.techraf

3 Answers

1
votes

If the verbose real stdout of apt is needed, you can use the command module to run a apt-get update.

Run this:

   -  hosts: target
      tasks:
      - name: 'apt test'
        command: apt-get update
        register: hello

      - debug: msg="{{ hello.stdout }}"

Ansible will warn you to use the apt module because it has certain ansible like functionalities.

0
votes

It seems that there is not a real output of the command for some operations as we might have hoped.

So when needed is related to the executed operation.

Many thanks to techraf.

Kr

0
votes

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.