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

18 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

  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 }}”

  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

Leave a Reply