skip to Main Content

I’m trying to install a list of packages excluding Nginx and Docker, as those requires a special treatment and are being installed in specific roles after that task.

An error is given when trying to install Nginx (which should be excluded).

To give more context over the proposed tests by @U880D I ran the following playbook:


- hosts: all
  become: true
  gather_facts: false

  vars:

    PACKAGES: ['docker', 'nginx', 'java', 'curl']
    EXCLUDE: ['docker', 'nginx']

  tasks:

  - name: Update system with exclude
    yum:
      name: "{{ PACKAGES }}"
      state: latest
      exclude: "{{ EXCLUDE }}"
    register: result
    ignore_errors: true

  - name: Show result
    debug:
      msg: "{{ result }}"

The ‘result’ register gave the following output:

ok: [template-delivery] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "failed": true,
        "failures": [
            "nginx All matches were filtered out by exclude filtering for argument: nginx"
        ],
        "msg": "Failed to install some of the specified packages",
        "rc": 1,
        "results": [],
        "warnings": [
            "Platform linux on host template-delivery is using the discovered Python interpreter at /usr/libexec/platform-python, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.13/reference_appendices/interpreter_discovery.html for more information."
        ]
    }
}

It seems that it is trying to install nginx anyways…

Then I modify the ‘EXCLUDE’ variable to:

EXCLUDE: ['docker']

and run again the playbook, the ‘result’ register output is:

ok: [template-delivery] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": true,
        "failed": false,
        "msg": "",
        "rc": 0,
        "results": [
            "Installed: nginx-1:1.22.0-1.el8.ngx.x86_64",
            "Installed: podman-docker-2:4.1.1-2.module+el8.6.0+20721+d8d917a9.noarch"
        ],
        "warnings": [
            "Platform linux on host template-delivery is using the discovered Python interpreter at /usr/libexec/platform-python, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.13/reference_appendices/interpreter_discovery.html for more information."
        ]
    }
}

Conclussions:

The exclude parameter seems not to be working properly, as it is trying to install the packages anyway.
For some reason, Nginx package is giving an error, but if I remove nginx from EXCLUDE list it is installing all the packages.

A github issue was opened but the solution proposed (modifying the documentation) is not resolving my issue.

Is there something I’m misunderstanding about the ‘exclude’ parameter or does it looks like a bug?

2

Answers


  1. exclude requires a string, not a list exclude: "{{ ', '.join(EXCLUDE) }}"

    Login or Signup to reply.
  2. You are asking the module to do contradictory things. You have explicitly asked for nginx to be installed, and also explicitly asked for nginx to be excluded from the list of available packages. This is not a request that yum is able to fulfill, so you get an error. If you do not want nginx to be installed, you should not ask for it to be installed.

    There are multiple ways to structure this; you can split your package list into multiple variables that are then combined:

    - hosts: all
      become: true
      gather_facts: false
      vars:
        packages_yum:
        - java
        - curl
        packages_nonyum:
        - docker
        - nginx
        packages: "{{ packages_yum | union(packages_nonyum) }}"
      tasks:
      - name: Update system
        yum:
          name: "{{ packages_yum }}"
          state: latest
    

    Or you can leave it as a list of packages and excluded packages, but filter the list that you pass to the module:

    - hosts: all
      become: true
      gather_facts: false
      vars:
        packages:
        - docker
        - nginx
        - java
        - curl
        excluded_packages:
        - docker
        - nginx
      tasks:
      - name: Update system
        yum:
          name: "{{ packages | difference(excluded_packages) }}"
          state: latest
    

    etc.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search