Skip to content

Example 7

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

playbook.yml

- name: Example playbook
  hosts: all
  become: true
  tasks:
    - name: Install vim on Ubuntu machines
      apt:
       name: vim
       state: present
      when: ansible_distribution == "Ubuntu"

Tip

Avoid using the command module for package installation, changes in systemd and other operations that can be automated using other Ansible modules.

Execute the following command

ansible-playbook playbook.yml

Bonus round

Create an Ansible Playbook that will install the Nginx web server on all machines (except provisioner). The package that provides the package is called nginx on both distributions.

  • use the apt or yum modules to download the package
  • on CentOS host you will need to install the epel-release package before installing Nginx
  • on Ubuntu hosts you will have to introduce changes to ufw (Ubuntu's firewall) - use the ufw module to allow a rule named Nginx HTTP
  • use the systemd module to start the Nginx webserver on all hosts
  • use curl from command line (outside of the playbook) or your web browser to check if the installation process was successfull