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 :)
Answer from jonashackt on Stack OverflowTo 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 :)
Some people have mentioned syntax that allows passing a list, but you don't need to special-case your code for apt or yum. Here is the command that uses the generic package manager module:
- name: Install packages
become: yes
package:
name:
- tmux
- rsync
- zsh
- wget
state: present
(Or state: latest.) This feature is only documented for Ansible 2.9, so it may be new.
Installing multiple packages with yum
Remotely install a software on multiple systems using ansible
How to install multiple packages but have the name of the packages in vars/packages.yml
Ansible download all files repo in Artifactory
You want the URI module if you're going to build a list https://docs.ansible.com/ansible/latest/modules/uri_module.html
And then, the https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API#ArtifactoryRESTAPI-PatternSearch resource to register the json *files* list and with_items using the get_url module
Alternatively, and most likely faster, you could use the https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API#ArtifactoryRESTAPI-RetrieveFolderorRepositoryArchive resource and the unarchive module https://docs.ansible.com/ansible/latest/modules/unarchive_module.html
More on reddit.comHow do I install a package using the APT module in Ansible?
Is it possible to install multiple packages at once with the APT module?
How can I install a package and ignore missing dependencies using the APT module?
Videos
My solution is to merge these into one play, as they do the same thing.
---
- name: php-fpm play
hosts: aws
become: true
vars:
- repo:
Debian:
- apt # already installed, but need something here
RedHat:
- epel-release
- packages:
Debian:
- apache2
#- nginx # cannot have 2 listening on port 80
- php-fpm
RedHat:
- httpd
#- nginx
- php-fpm
- services:
Debian: apache2
RedHat: httpd
tasks:
# TODO move update * task to different play
- name: Install repo
# Seperate package transaction for EPEL
# so it is available in the next task
package:
name: "{{ repo[ansible_os_family] }}"
- name: Install Web Server Packages
# Keyed by OS family fact to also support RHEL and Debian
package:
name: "{{ packages[ansible_os_family] }}"
state: latest
- name: Web Service Start
service:
name: "{{ services[ansible_os_family] }}"
state: restarted
enabled: yes
Can't have multiple servers listening on ports 80 and 443. I commented out nginx, as a task was mislabeled "Apache Service Start". If you want one of these proxying for the other or something, you will need to deploy a config file to change the ports.
Used the package: action which delegates to actual package manager. Enables a package install task to run on different OSes. Can't do update_cache that way, but yum doesn't need it when adding repos like apt does.
Vars structure is dicts of OS family specific values. This enables the package and service names to be indexed by facts. OS family, so this also works on RHEL and Debian in addition to CentOS and Ubuntu.
Indentation was wrong. Modules parameters need to be indented a level below task level directives like name:.
Can't include_tasks an entire play, that only works with import_playbook. include_tasks is easier when you have roles, which have a tasks directory for such files. (Play level tasks are a thing, but I am in favor of roles doing everything.)
There still is more work to make this useful.
When you need to use template to install configuration, EL puts configs in /etc/httpd/, while Debian is in /etc/apache2/.
Plenty of web server roles are open sourced, to look at if you want ideas. Check Galaxy.
Consider moving your tasks to roles, enables reuse.
Its' better if you take a role as example, here you have the fixed code:
---
- hosts: aws
remote_user: myuser
become: true
tasks:
- name: Performing Tasks for CentOS
include_tasks: centos.yml
when: ansible_facts['distribution'] == 'CentOS'
- name: Performing Tasks for Ubuntu
include_tasks: ubuntu.yml
when: ansible_facts['distribution'] == 'Ubuntu'
centos.yml tasks:
---
- name: installing httpd
yum: pkg=httpd state=present
ubuntu.yml tasks:
- name: installing apache2
apt: pkg=apache2 state=present
In the tasks files, you only need the tasks, without hosts,tasks and so on.
Hi mates, I need to install several packages usign the yum module.
The documentation says:
- name: Install a list of packages (suitable replacement for 2.11 loop deprecation warning)
yum:
name:
- nginx
- postgresql
- postgresql-server
state: present
- name: Install a list of packages with a list variable
yum:
name: "{{ packages }}"
vars:
packages:
- httpd
- httpd-toolsSo I did this playbook:
---
- hosts: all
tasks:
- name: Install required RAC packages
yum:
name:
- binutils-2.23.52.0.1-12.el7
- libSM-1.2.2-2.el7
- bzip2-libs-1.0.6-13.el7
- libstdc++-4.8.2-3.el7
- compat-libcap1-1.10-3.el7
- libstdc++-devel-4.8.2-3.el7
- compat-libcap1-1.10-7.el7
- libstdc++-devel-4.8.5-11.el7
- compat-libstdc++-33-3.2.3-71.el7
- libX11-1.6.0-2.1.el7
- compat-libstdc++-33-3.2.3-72.el7
- libXau-1.0.8-2.1.el7
- cpp-4.8.5-11.el7
- libXaw-1.0.12-5.el7
- elfutils-libelf-0.163-3.el7
- libxcb-1.9-5.el7
- elfutils-libelf-devel-0.163-3.el7
- libXi-1.7.2-1.el7
- expect-5.45-14.el7
- libXmu-1.1.2-2.el7
- gcc-4.8.5-11.el7
- libXpm-3.5.11-3.el7
- gcc-c++-4.8.5-11.el7
- libXt-1.1.4-6.1.el7
- glibc-2.17-36.el7
- libXtst-1.2.2-1.el7
- glibc-devel-2.17-157.el7
- make-3.82-19.el7
- glibc-devel-2.17-36.el7
- mpfr-3.1.1-4.el7
- glibc-headers-2.17-157.el7
- net-tools-2.0-0.17.20131004git.el7
- gzip-1.5-8.el7
- nfs-utils-1.3.0-0.21.el7
- kernel-headers-3.10.0-514.el7
- perl-Data-Dumper-2.145-3.el7
- ksh-20120801-26.el7
- psmisc x86_64 22.20-11.el7
- libaio-0.3.109-9.el7
- smartmontools-6.2-4.el7
- libaio-devel-0.3.109-13.el7
- sysstat-10.1.5-1.el7
- libaio-devel-0.3.109-9.el7
- unzip-6.0-16.el7
- libgcc-4.8.2-3.el7
- xorg-x11-xauth-1.0.9-1.el7
- libICE-1.0.9-2.el7
- xterm-295-3.el7
- libmpc-1.0.1-3.el7zip-3.0-11.el7
state: presentBut I get this error:
TASK [Install required RAC packages] ************************************************************************************************************************* fatal: [127.0.0.1]: FAILED! => changed=false msg: It appears that a space separated string of packages was passed in as an argument. To operate on several packages, pass a comma separated string of packages or a list of packages.
If I write this list as "item1,item2,item3" or "item1, item2, item3" I get an error too... I'm really not understanding where I'm wrong...
Can you help me, please?