This scenario shows:
- how to create ansible playbooks
- how to run "apt install" command in the playbook
- how to run "update" command in the playbook
- how to run "apt remove" command in the playbook
- how to run command according to distribution (when)
- You should have a look following lab, nodes that are created in that LAB, are using in ansible commands
- Create playbook to install apache2 app on nodes
nano install_apache.yml
# copy followings:
---
- hosts: all
become: true
tasks:
- name: install apache2 package
apt:
name: apache2
- Run playbook
ansible-playbook --ask-become-pass install_apache.yml
- Under Task "install apache2 package", status are changed, this means installing apache2 packet on the nodes successfully
- If you run again that command, it is seen that "ok" under the Task "install apache2 package", it means that there is no change
- When you enter the one of the nodes' IP on the browser, you can see that the apache2 server is installed on it
- If you enter "never-existed-tect-package" as apt name, you can see that it cannot install on the nodes, because it does not exist.
- Add "apt update" and install other packages
---
- hosts: all
become: true
tasks:
- name: update repository index
apt:
update_cache: yes
- name: install apache2 package
apt:
name: apache2
- name: add php support for apache
apt:
name: libapache2-mod-php
ansible-playbook --ask-become-pass install_apache.yml
- Add "state: latest" to install latest version of the app
---
- hosts: all
become: true
tasks:
- name: update repository index
apt:
update_cache: yes
- name: install apache2 package
apt:
name: apache2
state: latest
- name: add php support for apache
apt:
name: libapache2-mod-php
state: latest
- Create "remove_apache.yml" to uninstall apps.
- Use "state:absent" to uninstall.
---
- hosts: all
become: true
tasks:
- name: remove apache2 package
apt:
name: apache2
state: absent
- name: add php support for apache
apt:
name: libapache2-mod-php
state: absent
ansible-playbook --ask-become-pass remove_apache.yml
- When browsing IP to see whether apache2 works or not, it is seen that apache2 server was uninstalled.
- Add 'when: ansible-distribution == "Ubuntu"' into the install_apache.yaml file
- With 'when', it is possible to install command on specific distro (e.g. Ubuntu, Centos)
---
- hosts: all
become: true
tasks:
- name: update repository index
apt:
update_cache: yes
when: ansible_distribution in ["Debian", "Ubuntu"]
- name: install apache2 package
apt:
name: apache2
state: latest
when: ansible_distribution == "Ubuntu"
- name: add php support for apache
apt:
name: libapache2-mod-php
state: latest
when: ansible_distribution == "Ubuntu"
- To get more information about the specific node.
ansible all -m gather_facts --limit 172.21.79.85 | grep ansible_distribution
-
It is possible to use 'when' commands with other details (e.g. "ansible_distribution_version": "22.04")
-
Adding new tasks for 'CentOS' distribution
---
- hosts: all
become: true
tasks:
- name: update repository index
apt:
update_cache: yes
when: ansible_distribution in ["Debian", "Ubuntu"]
- name: install apache2 package
apt:
name: apache2
state: latest
when: ansible_distribution == "Ubuntu"
- name: add php support for apache
apt:
name: libapache2-mod-php
state: latest
when: ansible_distribution == "Ubuntu"
- name: update repository index
dnf:
update_cache: yes
when: ansible_distribution == "CentOS"
- name: install apache2 package
dnf:
name: httpd
state: latest
when: ansible_distribution == "CentOS"
- name: add php support for apache
dnf:
name: php
state: latest
when: ansible_distribution == "CentOS"
Run:
ansible-playbook --ask-become-pass install_apache.yml
- Tasks that are defined for 'CentOS' are skipped