DevOps Classroom Series – 13/Mar/2020

Playbooks for multiple linux distributions

  • Write the same playbook for multiple linux distributions
  • Write different playbooks for different linux distros

Different Playbooks for installing git,tree, nano, elinks

  • Ubuntu
---
- hosts: all
  become: yes
  tasks:
    - name: Install git
      apt:
        name: git
        update_cache: yes
        state: present
    - name: Install tree
      apt:
        name: tree
        state: present
    - name: Install nano
      apt:
        name: nano
        state: present
    - name: Install elinks
      apt:
        name: elinks
        state: present
        
---
- hosts: all
  become: yes
  tasks:
    - name: install git
      yum:
        name: git
        state: present
    - name: install tree
      yum:
        name: tree
        state: present
    - name: install nano
      yum:
        name: nano
        state: present
    - name: install elinks
      yum:
        name: elinks
        state: present
  • Now create a different inventory file for each distribution
  • Execute ansible-playbook commands separtely for each distribution
ansible-playbook -i hosts_ubuntu ex1-ubuntu.yaml
ansible-playbook -i hosts_centos ex1-centos.yaml

Lets make this better by writing one playbook and one inventory file

  • Use the generic package module package
---
- hosts: all
  become: yes
  tasks:
    - name: install git
      package: 
        name: git
        state: present
    - name: install tree
      package:
        name: tree
        state: present
    - name: install nano
      package:
        name: nano
        state: present
    - name: install elinks
      package:
        name: elinks
        state: present

Exercise – 2: Install apache on centos and ubuntu machines using ansible playbook

  • manual:
# ubuntu
sudo apt-get update
sudo apt-get install apache2 -y
sudo systemctl enable apache2
sudo systemctl start apache2

# centos
sudo yum install httpd -y
sudo systemctl enable httpd
sudo systemctl start httpd
  • To solve this problem, we have to understand ansible facts
  • Ansible facts gather information from the node where ansible is executed. These facts contain lot of information some of popular ones are
    • Operating SYtem
    • Free Memory
    • ethernet interfaces
    • free disk space
  • To collect facts ansible uses a module called as setup
  • Lets also learn how to execute ansible adhoc commands looking at parameters in module documentation page
  • We will be using service module and conditionals
  • Now lets look at playbook
---
- hosts: all
  become: yes
  tasks:
    - name: Install apache2
      apt:
        name: apache2
        update_cache: yes
        state: present
      when: ansible_os_family == "Debian"
    - name: Install httpd
      yum:
        name: httpd
        state: present
      when: ansible_os_family == "RedHat"
    - name: Enable apache2
      service:
        name: apache2
        enabled: yes
        state: started
      when: ansible_os_family == "Debian"
    - name: Enable httpd
      service:
        name: httpd
        enabled: yes
        state: started
      when: ansible_os_family == "RedHat"
    

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube