DevOps Classroom notes 08/Mar/2025

Parametrization

  • To make the playbook generic, we need to define variables.
  • Ansible variables can be passed from multiple locations
  • Lets look at the following playbook
---
- name: deploy website into apache webserver
  hosts: all
  become: yes
  vars:
    web_package_name: apache2
    unzip_package_name: unzip
    download_url: https://www.free-css.com/assets/files/free-css-templates/download/page291/goind.zip
  tasks:
    - name: install apache2
      ansible.builtin.apt:
        name: "{{ web_package_name  }}"
        update_cache: yes
        state: present
    - name: enable and ensure apache is started
      ansible.builtin.systemd_service:
        name: "{{ web_package_name  }}"
        enabled: yes
        state: started
    - name: ensure unzip is installed
      ansible.builtin.apt:
        name: "{{ unzip_package_name }}"
        state: present
    - name: download and extract website
      ansible.builtin.unarchive:
        src: "{{ download_url }}"
        dest: /tmp
        remote_src: yes
    - name: copy the html folder to naukri
      ansible.builtin.copy:
        src: /tmp/html/
        dest: /var/www/html/naukri
        remote_src: yes

  • In the above playbook we have defined the vars section
  • Now while executing playbook the command which we use ansible-playbook -i hosts web-deploy.yaml
  • Now if you want to change the variable
`ansible-playbook -e web_package=nginx -i hosts web-deploy.yaml`

Lets consider a scenario

  • You are asked to deploy website by executing above playbook on two hosts
  • one will use nginx
  • other will use apache2
  • Playbook
---
- name: deploy website into apache webserver
  hosts: all
  become: yes
  vars:
    unzip_package_name: unzip
    download_url: https://www.free-css.com/assets/files/free-css-templates/download/page291/goind.zip
  tasks:
    - name: display the web package name
      ansible.builtin.debug:
        msg: "web pacakage name is {{ web_package_name  }}"
    - name: "install web package"
      ansible.builtin.apt:
        name: "{{ web_package_name  }}"
        update_cache: yes
        state: present
    - name: "enable and ensure web package is started"
      ansible.builtin.systemd_service:
        name: "{{ web_package_name  }}"
        enabled: yes
        state: started
    - name: ensure unzip is installed
      ansible.builtin.apt:
        name: "{{ unzip_package_name }}"
        state: present
    - name: download and extract website
      ansible.builtin.unarchive:
        src: "{{ download_url }}"
        dest: /tmp
        remote_src: yes
    - name: copy the html folder to naukri
      ansible.builtin.copy:
        src: /tmp/html/
        dest: /var/www/html/naukri
        remote_src: yes

  • Inventory
172.31.4.82 web_package_name=nginx
localhost web_package_name=apache2

Inventory in ansible

  • Ansible on a broader note supports two types of inventories
    • static: For static we have two formats
      • ini
      • yaml
    • dynamic: To fetch the inventory dynamically we have two options
      • plugins
      • script

Static

  • ini: This format helps in writing static inventory
  • All server names/ips
172.31.4.82
localhost
  • Categorize into groups
[web]
localhost
172.31.4.82

[db]
172.31.4.83
172.31.4.84
localhost

[app]
172.31.4.85
172.31.4.86
localhost
  • We can add variables in the inventory file (not recommended)
[web]
localhost
172.31.4.82 web_package_name=nginx

[web:vars]
web_package_name=apache2
unzip_package_name=unzip
download_url= https://www.free-css.com/assets/files/free-css-templates/download/page291/goind.zip

[db]
172.31.4.83
172.31.4.84
localhost

[app]
172.31.4.85
172.31.4.86
localhost

Preview
* Writing variables relative to inventory file
* Refer Here for host_vars and group_vars

  • Inventories can also be written in YAML Format
web:
  hosts:
    localhost:
    172.31.4.82:
db:
  hosts:
    172.31.4.83:
    172.31.4.84:
    localhost:
app:
  hosts:
    172.31.4.85:
    172.31.4.86:
    localhost:


Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

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