Sobes.tech
Junior

What components and files does a role in DevOps consist of?

sobes.tech AI

Answer from AI

The role in DevOps, most often implemented through Ansible, consists of the following main components and files:

  • tasks/: Directory containing tasks executed by the role. The main file is main.yml.
    // tasks/main.yml
    - name: Install nginx
      apt:
        name: nginx
        state: present
    
  • handlers/: Directory containing event handlers. The main file is main.yml. Handlers are executed only when there is a change in the tasks that notify them.
    // handlers/main.yml
    - name: restart nginx
      service:
        name: nginx
        state: restarted
    
  • defaults/: Directory containing default variable values for the role. The main file is main.yml. These variables have the lowest priority.
    // defaults/main.yml
    nginx_port: 80
    
  • vars/: Directory containing role-specific variables. The main file is main.yml. These variables have a higher priority than those in defaults/.
    // vars/main.yml
    nginx_user: www-data
    
  • files/: Directory containing files that will be copied to target machines without changes.
    // tasks/main.yml
    - name: Copy static file
      copy:
        src: my_static_file.conf
        dest: /etc/nginx/conf.d/my_static_file.conf
    
  • templates/: Directory containing template files (usually in Jinja2 format) that will be copied to target machines after processing (variable substitution).
    // templates/nginx.conf.j2
    server {
        listen {{ nginx_port }};
        ...
    }
    
    // tasks/main.yml
    - name: Configure nginx
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: restart nginx
    
  • meta/: Directory containing role metadata, such as dependencies. The main file is main.yml.
    // meta/main.yml
    dependencies:
      - role: another_role
        variable: value
    
  • library/: (Optional) Directory for custom Ansible modules.
  • module_utils/: (Optional) Directory for custom utility scripts used in modules.
  • lookup_plugins/, filter_plugins/, etc.: (Optional) Directories for custom plugins of various types.

A typical role structure looks like this:

my_role/
├── tasks/
│   └── main.yml
├── handlers/
│   └── main.yml
├── defaults/
│   └── main.yml
├── vars/
│   └── main.yml
├── files/
│   └── my_static_file.conf
├── templates/
│   └── nginx.conf.j2
└── meta/
    └── main.yml

These components allow organizing automation of installation, configuration, and management of services and systems in a structured and reusable way.