Ansible apt-get install output

2019-04-08 17:51发布

问题:

I am using vagrant with an ansible playbook to automatically install a bunch of programs on an ubuntu image. One program is failing to install on the vagrant VM. In the Vagrant file I have

config.vm.provision :ansible do |ansible|
  ansible.verbose = "vvv"
  ansible.playbook = "provisioning/playbook.yml"
end

but the verbose output does not include the apt-get output. My playbook.yml looks like

---
- hosts: all
  sudo: true
  tasks:
    - name: get vi
      apt: state=latest name=vim  

How can I see the console output of an individual (or all) apt-get install's on the VM since ansible instead outputs each install in the format

TASK: [Install vim] *********************************************************** 
failed: [default] => {"failed": true}
...

回答1:

You can register to a variable the output of the apt module execution and then print it.

- hosts: localhost
  sudo: true
  tasks:
    - name: get vi
      apt: state=latest name=vim
      register: aptout

    # show the content of aptout var
    - debug: var=aptout


回答2:

Here is how to reproduce the stdout of apt

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

…with nice line breaks, thanks to .split('\n'), and omitting the last empty string with [:-1], all of which is Python string manipulation, of course.

"msg": [
    "Reading package lists...", 
    "Building dependency tree...", 
    "Reading state information...", 
    "Reading extended state information...", 
    "Initializing package states...", 
    "Building tag database...", 
    "No packages will be installed, upgraded, or removed.", 
    "0 packages upgraded, 0 newly installed, 0 to remove and 0 not upgraded.", 
    "Need to get 0 B of archives. After unpacking 0 B will be used.", 
    "Reading package lists...", 
    "Building dependency tree...", 
    "Reading state information...", 
    "Reading extended state information...", 
    "Initializing package states...", 
    "Building tag database..."
]


回答3:

In the version of ansible I'm using at the moment, ansible-playbook -v seems sufficient to get apt output. Admittedly I haven't tested failures. The output is in the form of JSON, which makes it a bit hard to read (as the other answer works around).

The Ansible version I tested was 2.3.2.0.