Sobes.tech
Middle+

What is your experience with handlers and role templates in DevOps?

sobes.tech AI

Answer from AI

Worked with handlers and templates within configuration management and orchestration systems.

In Ansible, I actively use handlers to perform actions dependent on task changes. For example, restarting a service after updating a configuration file.

- name: Update configuration file
  ansible.builtin.template:
    src: templates/myapp.conf.j2
    dest: /etc/myapp/myapp.conf
  notify:
    - Restart myapp service

- name: Restart myapp service
  ansible.builtin.service:
    name: myapp
    state: restarted
  listen:
    - "Restart myapp service"

I use templates to generate configuration files based on variables. This allows creating dynamic files tailored to specific environments or servers.

# templates/myapp.conf.j2
[General]
ListenPort = {{ listen_port }}
LogLevel = {{ log_level | default('info') }}

In Terraform, I apply templates to create dynamic data, such as user data scripts for VM initialization or generating configuration files for services.

data "template_file" "user_data" {
  template = file("${path.module}/user_data.sh")

  vars = {
    hostname = var.instance_hostname
    ip_address = var.instance_ip
  }
}

I work with Jinja2 and other templating engines integrated into DevOps tools. My experience includes using loops, conditions, filters, and variables in templates.

Often, the role structure in Ansible includes directories handlers/ and templates/ to organize relevant content. This improves readability and modularity of playbooks.