Skip to content

Latest commit

 

History

History
249 lines (176 loc) · 6.27 KB

Implement-First-Playbook.md

File metadata and controls

249 lines (176 loc) · 6.27 KB

LAB: Implement First Playbook

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)

Prerequisite

Steps

  • Create playbook to install apache2 app on nodes

"Apt Install" command in the playbook

nano install_apache.yml
# copy followings:
---

- hosts: all
  become: true
  tasks:

  - name: install apache2 package
    apt:
      name: apache2

image

image

  • 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

image

  • If you run again that command, it is seen that "ok" under the Task "install apache2 package", it means that there is no change

image

  • When you enter the one of the nodes' IP on the browser, you can see that the apache2 server is installed on it

image

  • 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.

image

image

"Update" command in the playbook

  • 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

image

ansible-playbook --ask-become-pass install_apache.yml

image

  • 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

image

"Apt remove" command in the playbook

  • 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

image

ansible-playbook --ask-become-pass remove_apache.yml

image

  • When browsing IP to see whether apache2 works or not, it is seen that apache2 server was uninstalled.

image

Command According to the Distribution ("when")

  • 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

image

  • 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

image

Reference