2

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

moocan
  • 111
  • 2
  • 11
  • @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 Jul 18 '18 at 21:03
  • @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 Jul 18 '18 at 21:55
  • 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 Jul 19 '18 at 06:11

5 Answers5

2

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.

leopold.talirz
  • 640
  • 7
  • 19
1

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.

Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37
0

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

moocan
  • 111
  • 2
  • 11
0

You can use directly stdout_lines, but you must make sure that apt did output something, for example, it installed something, otherwise that element is not defined. Here's a possible example:

- name: Install Gnome Packages
  become: yes
  apt:
    update_cache: yes
    state: latest
    pkg:
    - gnome-tweaks
    - dconf-editor
    - guake
  register: aptout

- debug: msg="{{ aptout.stdout_lines }}"
  when: aptout.stdout_lines is defined
lorenzo-bettini
  • 2,106
  • 1
  • 16
  • 11
0

Kindly run below ansible code to show to process of installation.

- name: Install packages using apt
  hosts: your_host
  become: yes
  tasks:
    - name: Install packages
      apt:
        name: "{{ item }}"
        state: present
      loop:
        - package1
        - package2
        - package3
      register: apt_output

    - name: Display apt output
      debug:
        var: apt_output
linux.cnf
  • 519
  • 6
  • 7