How to install a list of packages with Ansible

I’ve been developing Ansible playbooks to help manage our Ubuntu servers. I wanted to install a list of package on a server using Ansible:

The old syntax:

 - name: Install list of packages
   action: apt pkg={{item}} state=installed
   with_items:
        - package1
        - package2
        - package3
        - etc

The new syntax:

 - name: Install list of packages
   apt: name={{item}} state=installed
   with_items:
        - package1
        - package2
        - package3
        - etc

27 thoughts to “How to install a list of packages with Ansible”

  1. The syntax has changed, so now the recommended way to call a variable is:

    action: apt pkg={{item}} state=installed

    1. Example of this syntax – ‘|’ denotes the left margin, to show spacing

      |- name: Install virtualbox dependencies
      | apt:
      | name:
      | – dkms
      | – build-essential
      | – linux-headers-generic
      | state: present

      1. I’m really confused how you made this work with 2 ‘name’ fields in a single action. Especially when the method in the original post is the one in the Ansible docs.

        1. the – name: is just naming the “play” or action that ansible will take.

          |- name: Install virtualbox dependencies # you can name this whatever you want
          | apt: # the ansible module to run
          | name: # a required definition of the apt module
          | – dkms #
          | – build-essential # it can take list like that
          | – linux-headers-generic #
          | state: present # the state of the packages

  2. I try to do:

    – hosts: php
    sudo: yes

    tasks:

    – name: install packages
    apt: name={{ item }} update_cache=yes state=latest
    with_items:
    – nginx
    – php5-fpm

    But i have a problem:

    ERROR! Syntax Error while loading YAML.

    The error appears to have been in ‘/home/ansible-apache/php.yml’: line 7, column 1, but may
    be elsewhere in the file depending on the exact syntax problem.

    The offending line appears to be:

    – name: install packages
    apt: name={{item}} update_cache=yes state=latest
    ^ here
    We could be wrong, but this one looks like it might be an issue with
    missing quotes. Always quote template expression brackets when they
    start a value. For instance:

    with_items:
    – {{ foo }}

    Should be written as:

    with_items:
    – “{{ foo }}”

    1. You must indent ‘apt’ so that it starts below ‘name’ not below ‘- name’

      Example:

      – name: apt install initial packages
      become: yes
      apt:
      name:
      – findutils
      – fortune
      – pwgen
      – sudo
      – vim
      update_cache: yes

      1. Sorry trailing blanks are removed.

        |- name: apt install initial packages
        | become: yes
        | apt:
        | name:
        | – findutils
        | – fortune
        | – pwgen
        | – sudo
        | – vim
        | update_cache: yes

  3. Hi ALL,

    I want to check multiple rpms and install if not present. (rhel6)

    vars:
    – is_gfs2: rpm -q gfs2-utils
    – is_lvm2: rpm -q lvm2-cluster
    – is_unzip: rpm -q unzip
    – gfs2_rpm: gfs2-utils
    – lvm2_rpm: lvm2-cluster

    tasks:
    ### UNZIP RPM Check ####
    – name: Check if unzip.rpm is installed
    # command: “{{ is_unzip }}”
    raw: “{{ item.pkg }}”
    with_items:
    – { pkg: “{{ is_gfs2 }}” }
    – { pkg: “{{is_lvm2 }}” }
    ignore_errors: True
    register: rpm_check
    – name: Installing Unzip
    # yum: name= “{{ unzip }}” state=latest
    yum: name= “{{ item.pkg }}” state=latest
    with_items:
    – { pkg: “{{ gfs2_rpm }} ” }
    – { pkg: “{{ lvm2_rpm }} ” }
    when: rpm_check.stdout.find(‘is not installed’) != -1
    register: yum_result
    changed_when: yum_result.rc == 0 or yum_result == 1 or yum_result.rc == 2
    failed_when: yum_result.rc != 0 and yum_result != 1 and yum_result.rc != 2

    Getting below error:
    TASK: [Installing Unzip] ******************************************************
    fatal: [xxxxxxx] => error while evaluating conditional: rpm_check.stdout.find(‘is not installed’) != -1
    fatal: [yyyyyyy] => error while evaluating conditional: rpm_check.stdout.find(‘is not installed’) != -1
    fatal: [zzzzzzz] => error while evaluating conditional: rpm_check.stdout.find(‘is not installed’) != -1

    FATAL: all hosts have already failed — aborting

  4. Create a playbook and install below softwares in ubuntu …Please help me tried all above options and not working

    apache2

    sqlite3

    git

Leave a Reply