Skip to content

Latest commit

 

History

History
123 lines (94 loc) · 2.81 KB

Refactoring-Playbook.md

File metadata and controls

123 lines (94 loc) · 2.81 KB

LAB: Refactoring / Improving Playbook

This scenario shows:

  • how to refactor / improve playbook

Prerequisite

Steps

---

- 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"

Consolidate same modules tasks into one task

  • Consolidate 'apt' and 'dnf' modules into one group both for Ubuntu and CentOS.
  • For both of them, they install two packages and update repository index.
---

- hosts: all
  become: true
  tasks:

  - name: update repository index, install apache2 and php package for Ubuntu
    apt:
      name:
        - apache2
        - libapache2-mod-php
      state: latest
      update_cache: yes
    when: ansible_distribution == "Ubuntu"

  - name: update repository index, install apache2 and php package for CentOS
    dnf:
      name:
        - httpd
        - php
      state: latest
      update_cache: yes
    when: ansible_distribution == "CentOS"

Consolidate into one task with Variables

  • Update install_apache.yml file and add variables: apache_package, php_package
  • Change 'apt' and 'dnf' to 'package'
---

- hosts: all
  become: true
  tasks:

  - name: update repository index, install apache and php packages
    package:
      name:
        - "{{ apache_package }}"
        - "{{ php_package }}"
      state: latest
      update_cache: yes
  • Update inventory and add variables
172.21.79.85  apache_package=apache2 php_package=libapache2-mod-php
172.21.76.101 apache_package=apache2 php_package=libapache2-mod-php
172.21.76.102 apache_package=httpd php_package=php
  • Run:
ansible-playbook --ask-become-pass install_apache.yml

image