This scenario shows:
- how to target specific nodes
- You should have a look following lab, nodes that are created in that LAB, are using in ansible commands
- Update inventory file.
- Make groups by giving names with '[]'.
[web_servers]
172.21.79.85
[database_servers]
172.21.76.101
- Create 'site.yml' file
- To prioritize the tasks, for updating, 'pre_tasks' is used
- For different hosts, 3 different host groups are created: all, web_servers and database_servers
---
- hosts: all
become: true
pre_tasks:
- name: install updates (CentOS)
dnf:
update_only: yes
update_cache: yes
when: ansible_distribution == "CentOS"
- name: install updates (Ubuntu)
apt:
upgrade: dist
update_cache: yes
when: ansible_distribution == "Ubuntu"
- hosts: web_servers
become: true
tasks:
- name: install apache and php (CentOS)
dnf:
name:
- httpd
- php
state: latest
when: ansible_distribution == "CentOS"
- name: install apache and php (Ubuntu)
apt:
name:
- apache2
- libapache2-mod-php
state: latest
when: ansible_distribution == "Ubuntu"
- hosts: database_servers
become: true
tasks:
- name: install MariaDB (CentOS)
dnf:
name: mariadb
state: latest
when: ansible_distribution == "CentOS"
- name: install MariaDB (Ubuntu)
apt:
name: mariadb-server
state: latest
when: ansible_distribution == "Ubuntu"
- Run
ansible-playbook --ask-become-pass site.yml
- When we control whether mariadb is installed or not on database_servers (node2), it can be seen that it was installed on node2.
multipass shell node2
systemctl status mariadb