How to install software packages with an Ansible playbook

Learn how to install new software packages on all your managed hosts with a single Ansible playbook.

Ansible is a popular automation tool used by sysadmins and developers to get computers in a specific state. Ansible modules are, in a way, what commands are to a Linux computer. They provide solutions to specific problems, and one common task when maintaining computers is keeping them updated and consistent. In this article, I show you how to install software packages with Ansible.

Manages packages with the yum package manager

---
- hosts: localhost
  become: true
  become_user: root

  tasks:
    - name: Install a single package 
      yum:
        name: httpd
        state: present

---
- hosts: localhost
  become: true
  become_user: root

  tasks:
    - name: Install multiple packages using a list
      yum:
        name:
         - nginx
         - postgresql
         - postgresql-server
        state: present

---
- hosts: localhost
  become: true
  become_user: root

  tasks:
    - name: Install multiple packages with a list variable
      yum:
        name: "{{ packages }}"
      vars:
        packages:
        - httpd
        - httpd-tools

Manages packages with the dnf package manager

---
- hosts: localhost
  become: true
  become_user: root

  tasks:
    - name: Install a single package 
       dnf:
        name: httpd
        state: present

---
- hosts: localhost
  become: true
  become_user: root

  tasks:
    - name: Install multiple packages using a list
      dnf:
       name:
        - nginx
        - postgresql
        - postgresql-server
       state: present

Manages packages with the apt package manager

---
- hosts: localhost
  become: true
  become_user: root

  tasks:
    - name: Install a single package (state=present is optional)
       apt:
        name: apache2
        state: present

---
- hosts: localhost
  become: true
  become_user: root

  tasks:
    - name: Install multiple packages using a list
      apk:
       pkg:
       - nginx
       - zfsutils-linux
       - openjdk-6-jdk

Manages packages with win_package package manager

---
- hosts: localhost
  become: true
  become_user: root

  tasks:
    - name: Install the Visual C thingy
      win_package:
        path: http://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x64.exe
        product_id: '{CF2BEA3C-26EA-32F8-AA9B-331F7E34BA97}'
        arguments: /install /passive /norestart

---
- hosts: localhost
  become: true
  become_user: root

  tasks:
    - name: Install an MSIX package for the current user
      win_package:
        path: C:\Installers\Calculator.msix  # Can be .appx, .msixbundle, or .appxbundle
        state: present

Leave a Reply

Your email address will not be published. Required fields are marked *