So installing multiple apps in your playbook helps consolidate your playbook, makes it run faster and it is easier to read. Below is an example.
sudo nano install-multiple-apps-ubuntu.yml
---
- hosts: all
become: true
tasks:
- name: install vlc and apache2 Ubuntu
apt:
name:
- vlc
- apache2
state: latest
when: ansible_distribution == 'Ubuntu'
ansible-playbook --ask-become-pass install-multiple-apps-ubuntu.yml
The below variable example is using package instead of apt or dnf as package runs what ever the package installer is for the operating system. What we are doing is defying a name in the playbook and telling it what to install in the inventory.ini file.
sudo nano using-variables.yml
---
- hosts: all
become: true
tasks:
- name: install apache2 Ubuntu and httpd centos
package:
name:
- "{{ apache_package }}"
sudo nano inventory.ini
#centos
192.168.1.151 apache_package=httpd
#Ubuntu
192.168.1.130 apache_package=apache2
ansible-playbook --ask-become-pass using-variables.yml
So you can group server type in the inventory.ini file and the playbook to specify the group type. See below example
cat inventory.ini
#centos
[fileservers]
192.168.1.151 apache_package=httpd
#Ubuntu
[webservers]
192.168.1.130 apache_package=apache2
sudo nano targeting-groupservers.yml
---
- hosts: webservers
become: true
tasks:
- name: install apache2 on Ubuntu
apt:
name: apache2
state: latest
when: ansible_distribution == 'Ubuntu'
- hosts: fileservers
become: true
tasks:
- name: install samba on Centos
dnf:
name: samba
state: latest
when: ansible_distribution == 'CentOS'
ansible-playbook --ask-become-pass targeting-groupservers.yml
Below are outputs from ansible control node, we are looking at changed value


