Search code examples
ansiblecentosrpmansible-2.xrhel

Ansible task is failing without any error


I have an Ansible task running on RHEL and CentOS machines.

- name: Git version (Fedora)
  command: rpm -qi git
  args:
    warn: no
  register: fedora_git
  tags:
    - git

But this task is getting failed without any error.

FAILED! => {"changed": true, "cmd": ["rpm", "-qi", "git"], "delta": "0:00:00.066596", "end": "2023-05-23 01:28:16.302918", "msg": "non-zero return code", "rc": 1, "start": "2023-05-23 01:28:16.236322", "stderr": "", "stderr_lines": [], "stdout": "package git is not installed", "stdout_lines": ["package git is not installed"]}

Tried with shell module, but getting the same error.


Solution

  • In Ansible it is generally recommended to use tasks specific modules instead of command or shell if available and possible. In your case the package_facts module in order to get package information as facts.

    A minimal Example playbook

    ---
    - hosts: test
      become: false
      gather_facts: false
    
      tasks:
    
      - name: Gather the package facts
        ansible.builtin.package_facts:
          manager: auto
    
      - name: Show Facts
        ansible.builtin.debug:
          msg: "{{ ansible_facts.packages }}"
    
      - name: Print the package names only
        ansible.builtin.debug:
          msg: "{{ ansible_facts.packages | dict2items | map(attribute='key') }}"
    

    will result into a list output of the installed packages, or a task like

      - name: Show Git Facts
        ansible.builtin.debug:
          msg: "{{ ansible_facts.packages.git }}"
        when: ansible_facts.packages.git is defined # means installed
    

    will result into an output of

    TASK [Show Git Facts] ******
    ok: [localhost] =>
      msg:
      - arch: x86_64
        epoch: null
        name: git
        release: 24.el7_9
        source: rpm
        version: 1.8.3.1
    

    For other format and information you need to make further adjustments.


    But this task is getting FAILED without any error. ... Tried with shell module, but getting the same error.

    Right, this is the expected behavior and result for querying the local package database for information of not installed packages and caused by Return Code (RC).

    Whereby a command like

    rpm -qi git; echo $?
    Name        : git
    Version     : 1.8.3.1
    Release     : 24.el7_9
    Architecture: x86_64
    Install Date: Fri 14 Apr 2023 11:04:29 AM CEST
    Group       : Development/Tools
    Size        : 23265551
    License     : GPLv2
    Signature   : RSA/SHA256, Wed 22 Feb 2023 09:30:47 AM CET, Key ID 199e2f91fd431d51
    Source RPM  : git-1.8.3.1-24.el7_9.src.rpm
    Build Date  : Tue 21 Feb 2023 05:26:24 PM CET
    Build Host  : x86-vm-37.build.eng.bos.redhat.com
    Relocations : (not relocatable)
    Packager    : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
    Vendor      : Red Hat, Inc.
    URL         : http://git-scm.com/
    Summary     : Fast Version Control System
    Description :
    Git is a fast, scalable, distributed revision control system with an
    unusually rich command set that provides both high-level operations
    and full access to internals.
    
    The git rpm installs the core tools with minimal dependencies.  To
    install all git packages, including tools for integrating with other
    SCMs, install the git-all meta-package.
    0
    

    will provide package information and a "rc": 0, a command like

    rpm -qi not-installed; echo $?
    package not-installed is not installed
    1
    

    will not and provide a "rc": 1.

    Further Reading