janina
janina

Reputation: 13

installing package using loop in ansible

I need to use with_items loop to install apache2, sqlite3, and git in Ansible. I'm trying to use the below code but it seems like nothing is happening.

---
- hosts: all
  sudo: yes
  name: install apache2, sqlite3, git on remote server
  tasks:
  - name: Install list of packages
    action: apt pkg={{item}} state=installed
    with_items:
      - apache2
      - sqlite3
      - git

Upvotes: 1

Views: 6231

Answers (2)

Math
Math

Reputation: 31

you have to place the variable item inside the double quotes... Try this code it'll work:

---
- name: install apache2, sqlite3, git on remote servers
  hosts: all
  become: true
  tasks:
    - name: Install packages
      package:
        name: "{{item}}"
        state: present
      loop:
        - apache2
        - sqlite3
        - git

Upvotes: 2

Vladimir Botka
Vladimir Botka

Reputation: 68179

Try

---
- name: install apache2, sqlite3, git on remote servers
  hosts: all
  sudo: true
  tasks:
    - name: Install packages
      package:
        name: {{ item }}
        state: present
      loop:
        - apache2
        - sqlite3
        - git

See package – Generic OS package manager

"This module actually calls the pertinent package modules for each system (apt, yum, etc)."

See apt – Manages apt-packages if you need apt specific attributes.

Upvotes: 0

Related Questions