How to Fix the Ansible 'No package matching' Error

beginner🔧 Ansible2026-05-19| Ubuntu/Debian (apt), CentOS/RHEL/Fedora (yum/dnf), Ansible 2.9+

Error Message

No package matching 'nginx' is available
#ansible#devops#linux#apt#yum

The ProblemYou kick off an Ansible playbook to provision a new web server. Everything looks green until the task to install Nginx or Redis hits a wall. Even though you know the package exists, Ansible reports it can't find a match.

The error usually halts your deployment with this output:

fatal: [server_ip]: FAILED! => {"changed": false, "msg": "No package matching 'nginx' is available"}

This is a common hurdle on freshly launched cloud instances, like an AWS EC2 Ubuntu 22.04 image, where the local package index is often empty or outdated by default.

Why Ansible can't find your packageAnsible doesn't scan the live internet for software. Instead, it relies on the local metadata cache stored on the remote host. If that index is a few days old—or has never been populated—the package manager won't see the software you're asking for.

Common culprits include:

  • Stale Cache: The server hasn't run an update recently and doesn't know about current package versions.- Missing Repositories: You are trying to install software like nginx on RHEL without the EPEL repository enabled.- Naming Mismatches: The package is called apache2 on Debian but httpd on Red Hat.- Network Restrictions: Firewall rules or missing proxy settings are blocking the server from reaching the mirrors.## The Quick Fix: Refresh the Package CacheThe fastest solution is to force Ansible to update the package index before it tries the installation. By default, Ansible skips this step to save about 10–30 seconds of execution time.

For Ubuntu/Debian (apt)Add update_cache: yes to your task. This mirrors the behavior of running apt-get update manually.

- name: Install nginx and update cache
  ansible.builtin.apt:
    name: nginx
    state: present
    update_cache: yes

For CentOS/RHEL (yum/dnf)While yum generally handles metadata better than apt, you can still force a refresh to ensure the task pulls from the latest repository data:

- name: Install nginx and refresh yum metadata
  ansible.builtin.yum:
    name: nginx
    state: present
    update_cache: yes

The Permanent Fix: Adding RepositoriesIf a cache refresh doesn't solve it, the package likely isn't in the default OS repositories. For instance, a standard CentOS 7 or 8 stream installation does not include Nginx in its base repo.

Enabling EPEL on RHEL/CentOSYou must install the Extra Packages for Enterprise Linux (EPEL) repository first. This adds thousands of standard open-source packages to your available list.

- name: Install EPEL repository
  ansible.builtin.yum:
    name: epel-release
    state: present

- name: Install nginx
  ansible.builtin.yum:
    name: nginx
    state: present
    update_cache: yes

Adding PPAs on UbuntuIf you need a specific version, such as Nginx Mainline or Certbot, you may need a Personal Package Archive (PPA):

- name: Add Nginx stable PPA
  ansible.builtin.apt_repository:
    repo: 'ppa:nginx/stable'
    state: present
    update_cache: yes

- name: Install nginx
  ansible.builtin.apt:
    name: nginx
    state: present

Handling Cross-Platform Package NamesSometimes the error occurs because of a simple naming difference. Don't hardcode names if you manage multiple OS families. Use variables to dynamically select the right name for the right system.

- name: Set package name based on OS
  set_fact:
    web_server_package: "{{ 'apache2' if ansible_os_family == 'Debian' else 'httpd' }}"

- name: Install web server
  ansible.builtin.package:
    name: "{{ web_server_package }}"
    state: present

Manual VerificationIf the playbook still fails, log into the remote host to check the package status manually. This confirms if the issue is with your Ansible logic or the server's network.

On Debian/Ubuntu:```

Check if the package is known to apt

apt-cache policy nginx


If the output shows `Candidate: (none)`, your repository configuration is still broken.
### On CentOS/RHEL:```
# List available packages matching nginx
yum list available | grep nginx

Summary Checklist- Is update_cache: yes included in the task?- For RHEL/CentOS systems, is epel-release active?- Are you using the correct package name for that specific Linux distribution?- Does the server have outbound access to port 80/443 to reach the mirrors?

Related Error Notes