DevOps Classroom notes 06/Nov/2026

Ansible variable

  • variables are store values that can be reuse across playbook, task, role.

variables define in directly on playbook

- name: "Scholar Website"
  hosts: all
  vars:
    app_name: "Scholar Website"
    depend_packages:
      - wget
      - unzip
      - httpd
    package_name: "httpd"
    service_name: "httpd"
    template_name: "templatemo_586_scholar"
    website_url: "https://templatemo.com/tm-zip-files-2020/{{ template_name }}.zip"
    username: "apache"
    file_permissions: "0755" 
  become: yes

  tasks:
    - name: "Install depend packages"
      ansible.builtin.dnf:
        name: "{{ depend_packages }}"
        state: present
        update_cache: yes

    - name: "Install Apache2"
      ansible.builtin.dnf:
        name: "{{ package_name }}"
        state: present
      notify: "Restart Apache2"

    - name: "Download website template"
      ansible.builtin.get_url:
        url: "{{ website_url }}"
        dest: /tmp/{{ template_name }}.zip
        mode: "{{ file_permissions }}"

    - name: "Unzip website template"
      ansible.builtin.unarchive:
        src: /tmp/{{ template_name }}.zip
        dest: /tmp/
        remote_src: yes

    - name: "Ensure Apache document root exists"
      ansible.builtin.file:
        path: /var/www/html
        state: directory
        owner: "{{ username }}"
        group: "{{ username }}"
        mode: "{{ file_permissions }}"

    - name: "Copy website files to deploy {{ app_name }}"
      ansible.builtin.copy:
        src: /tmp/{{ template_name }}/
        dest: /var/www/html/
        remote_src: yes
        owner: "{{ username }}"
        group: "{{ username }}"
        mode: "{{ file_permissions }}"
      notify: "Restart Apache2"
  
  handlers:
    - name: "Restart Apache2"
      ansible.builtin.systemd_service:
        name: "{{ service_name }}"
        state: restarted

run playbook

ansible-playbook -i hosts redhat-school.yaml  -e "ansible_user=ansible"

Override variable in cmd line

nsible-playbook -i hosts redhat-school.yaml  -e "ansible_user=ansible" -e "app_name=devops-schools"

define separate variable file

# Vars/main.yaml
app_name: "Scholar Website"
depend_packages:
  - wget
  - unzip
  - httpd
package_name: "httpd"
service_name: "httpd"
template_name: "templatemo_586_scholar"
website_url: "https://templatemo.com/tm-zip-files-2020/{{ template_name }}.zip"
username: "apache"
file_permissions: "0755" 
ansible-playbook -i hosts redhat-school.yaml  -e "ansible_user=ansible" -e "app_name=devops-schools" -e "@vars/main.yaml"

-e @pathofvariablefile.yaml this way we need pass variable

want to define variable file in yaml file

- name: "Scholar Website"
  hosts: all
  vars_files:
    - vars/main.yaml

vars_Prompt

  • vars_Prompt will ask inputs when running playbook manual need to pass value
- name: "Scholar Website"
  hosts: all
  vars_prompt:
    - name: "username"
      prompt: "Enter the username to deploy the website"
      private: no

Set_facts

  • added variable to ansible facts
  • added some value out from task and use same value in another tasks
- name: "Scholar Website"
  hosts: all
  vars_files:
    - vars/main.yaml
  vars:
    application_port: "80"
  become: yes

  tasks:
    - name: "Install depend packages"
      ansible.builtin.dnf:
        name: "{{ depend_packages }}"
        state: present
        update_cache: yes

    - name: "Install Apache2"
      ansible.builtin.dnf:
        name: "{{ package_name }}"
        state: present
      notify: "Restart Apache2"

    - name: "Download website template"
      ansible.builtin.get_url:
        url: "{{ website_url }}"
        dest: /tmp/{{ template_name }}.zip
        mode: "{{ file_permissions }}"

    - name: "Unzip website template"
      ansible.builtin.unarchive:
        src: /tmp/{{ template_name }}.zip
        dest: /tmp/
        remote_src: yes

    - name: "Ensure Apache document root exists"
      ansible.builtin.file:
        path: /var/www/html
        state: directory
        owner: "{{ username }}"
        group: "{{ username }}"
        mode: "{{ file_permissions }}"

    - name: "Copy website files to deploy {{ app_name }}"
      ansible.builtin.copy:
        src: /tmp/{{ template_name }}/
        dest: /var/www/html/
        remote_src: yes
        owner: "{{ username }}"
        group: "{{ username }}"
        mode: "{{ file_permissions }}"
      notify: "Restart Apache2"

    - name: set facts
      ansible.builtin.set_fact:
        full_url: "http://{{ ansible_hostname }}:{{ application_port }}"
    
    - name: endpoint details
      debug:
        msg: "Endpoint is {{ full_url }}"
  
  handlers:
    - name: "Restart Apache2"
      ansible.builtin.systemd_service:
        name: "{{ service_name }}"
        state: restarted

ansible_hostname is default facts value and full_url set facts

TASK [endpoint details] ********************************************************
ok: [172.31.23.92] => {
    "msg": "Endpoint is http://ip-172-31-23-92:80"
}

ansible facts offical docs

Task:

  1. create Tomcat10 installation playbook
    1. redhat Tomcat-redhat installtion click here
    2. ubuntu Tomcat-ubuntu installtion click here
  2. make nop playbook re-use
    1. https://docs.nopcommerce.com/en/installation-and-upgrading/installing-nopcommerce/installing-on-linux.html

Leave a comment

Turn on JavaScript to leave a comment.

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

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company
Exit mobile version
%%footer%%