Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing multiple packages in Ansible

Tags:

ansible

I have this task in Ansible:

- name: Install mongodb
  yum:
    name:
    - "mongodb-org-{{ mongodb_version }}"
    - "mongodb-org-server-{{ mongodb_version }}"
    - "mongodb-org-mongos-{{ mongodb_version }}"
    - "mongodb-org-shell-{{ mongodb_version }}"
    - "mongodb-org-tools-{{ mongodb_version }}"
    state: present
  notify: Restart mongodb

Is there a way I can indicate the version without having to use a loop like this? What is a more "elegant" way of writing this?

- name: Install mongodb
  yum:
    name: "{{ item }}-{{ mongodb_version }}"
    state: present
    loop:
    - mongodb-org-server
    - mongodb-org-mongos
    - mongodb-org-shell
    - mongodb-org-tools
  notify: Restart mongodb
like image 385
FrancisV Avatar asked Mar 01 '19 11:03

FrancisV


2 Answers

To my surprise I didn't find the simplest solution in all the answers, so here it is. Referring to the question title Installing multiple packages in Ansible this is (using the yum module):

- name: Install MongoDB
  yum:
    name:
      - mongodb-org-server
      - mongodb-org-mongos
      - mongodb-org-shell
      - mongodb-org-tools 
    state: latest
    update_cache: true

Or with the apt module:

- name: Install MongoDB
  apt:
    pkg:
      - mongodb-org-server
      - mongodb-org-mongos
      - mongodb-org-shell
      - mongodb-org-tools 
    state: latest
    update_cache: true

Both modules support inline lists!

The second part of your question is how to integrate specific version numbers into the package lists. No problem - simply remove the state: latest (since using specific version numbers together with state: latest would raise errors) and add the version numbers to the package names using a preceding = like this:

- name: Install MongoDB
  yum:
    name:
      - mongodb-org-server=1:3.6.3-0centos1.1
      - mongodb-org-mongos=1:3.6.3-0centos1.1
      - mongodb-org-shell=1:3.6.3-0centos1.1
      - mongodb-org-tools=1:3.6.3-0centos1.1 
    update_cache: true

You could also optimize further and template the version numbers. In this case don't forget to add quotation marks :)

like image 191
jonashackt Avatar answered Oct 07 '22 11:10

jonashackt


Make this into an Ansible Role called mongo, resulting in the directory structure:

playbook.yml
roles
|-- mongo
|  |-- defaults
|  |    |-- main.yml
|  |
|  |-- tasks
|  |    |-- main.yml
|  |
|  |-- handlers
|  |    |-- main.yml

Add the required MongoDB packages and version into the default variables file roles/mongo/defaults/main.yml:

mongo_version: 4.0
mongo_packages:
- mongodb-org-server
- mongodb-org-mongos
- mongodb-org-shell
- mongodb-org-tools

Re-write the task in the roles/mongo/tasks/main.yml file:

- name: Install mongodb
  yum:
    name: "{{ item }}-{{ mongo_version }}"
    state: present
  with_items: "{{ mongo_packages }}"
  notify: Restart mongodb

Add your handler logic to restart MongoDB in the file roles/mongo/handlers/main.yml.

Write a Playbook called playbook.yml that calls the role.

---
- hosts: all
  roles:
  - mongo
like image 9
Charlie Avatar answered Oct 07 '22 13:10

Charlie