copy-deployment-configs.yaml
Overview
This Ansible playbook manages deployment configurations by conditionally generating and updating several key configuration files within a specified directory (BK_DIR). It primarily focuses on verifying the presence of a JSON configuration file, setting a fact accordingly, and templating configuration files such as Docker Compose, Aerospike, and Logrotate based on deployment conditions and upgrade flags.
The file automates and controls deployment configuration updates to ensure that necessary files are created or refreshed only under specific conditions, such as during full deployments or partial upgrades.
Tasks and Their Functionality
1. Check if bk_set.json Exists
- name: Check if bk_set.json exists
ansible.builtin.stat:
path: "{{ BK_DIR }}/bk-configs/bk_set.json"
register: bk_set_file_stat
Purpose: Uses the
statmodule to check for the existence of the filebk_set.jsoninside thebk-configsfolder within the base directoryBK_DIR.Output: The result is registered in the variable
bk_set_file_stat.Usage: This check is foundational for subsequent conditional logic that depends on the presence of this file.
2. Set HAS_BK_SET_FILE Fact
- name: Set HAS_BK_SET_FILE fact
ansible.builtin.set_fact:
HAS_BK_SET_FILE: "{{ bk_set_file_stat.stat.exists }}"
Purpose: Sets a boolean Ansible fact named
HAS_BK_SET_FILEbased on whetherbk_set.jsonexists (trueorfalse).Parameters:
No parameters; it uses the registered variable from the previous step.
Usage: This fact can be used in other tasks or roles to conditionally execute actions depending on the presence of the configuration file.
3. Production Compose File Generation
- name: Production compose
ansible.builtin.template:
src: templates/docker-compose.j2
dest: "{{ BK_DIR }}/docker-compose.yaml"
mode: "0644"
register: compose
when: not FAST_UPDATE or (UPGRADE is defined and ('compose' in UPGRADE or 'node' in UPGRADE or 'compose-add' in UPGRADE))
Purpose: Generates or updates the Docker Compose YAML file from a Jinja2 template.
Parameters:
src: The Jinja2 template filedocker-compose.j2located in thetemplatesdirectory.dest: The target path for the rendered compose file.mode: File permissions set to0644(readable by owner and others).
Conditional Execution:
Runs if
FAST_UPDATEis not set, or if an upgrade is defined and includes certain components (compose,node, orcompose-add).
Output: The result is registered into the variable
compose.Usage Example:
Used during full deployment or when specific components require a compose file update in an upgrade.
Interaction: The generated compose file is critical for container orchestration and service setup.
4. Aerospike Configuration Generation
- name: Aerospike configuration
ansible.builtin.template:
src: templates/aerospike.conf.j2
dest: "{{ BK_DIR }}/aerospike-config/aerospike.conf"
mode: "0644"
register: aerospike
when: not FAST_UPDATE or (UPGRADE is defined and 'aerospike' in UPGRADE)
Purpose: Creates or updates the Aerospike database configuration file from its respective template.
Parameters:
src: Template fileaerospike.conf.j2.dest: Destination path for the Aerospike configuration file.mode: File permissions set to0644.
Conditional Execution:
Runs unless
FAST_UPDATEis true, or if an upgrade includesaerospike.
Output: The task result is stored in
aerospike.Usage:
Ensures the Aerospike configuration is up to date during deployments or upgrades affecting the database.
Interaction: This file directly impacts Aerospike server behavior and performance.
5. Log Rotate Configuration Generation
- name: Log rotate configuration
ansible.builtin.template:
src: templates/logrotate.j2
dest: "{{ BK_DIR }}/logrotate.sh"
mode: "0755"
register: logrotate
when: not FAST_UPDATE or (UPGRADE is defined and 'logrotate' in UPGRADE)
Purpose: Generates or updates the logrotate shell script from a template.
Parameters:
src: The logrotate script template.dest: The output script file.mode: Executable permissions0755.
Conditional Execution:
Runs if a full deployment or an upgrade includes logrotate.
Output: Stores the task result in
logrotate.Usage:
Automates log file rotation setup, critical for log management and disk space optimization.
Interaction: The generated script is used by system cron jobs or manual execution to rotate logs.
Implementation Details and Algorithms
Conditional Execution Using
when: Each templating task employs conditional expressions to minimize unnecessary file updates. This reduces downtime and resource consumption during incremental upgrades or fast updates.Fact Setting: The
set_factmodule leverages the existence check outcome, enabling other roles or tasks to adapt behavior based on available configuration files.Use of Jinja2 Templates: Templates allow dynamic generation of configuration files based on variables and system state, promoting reusability and maintainability.
Interaction with Other System Components
Templates Directory: The playbook depends on Jinja2 templates (
docker-compose.j2,aerospike.conf.j2,logrotate.j2) located in thetemplatesfolder, which define the content structure of generated files.BK_DIRVariable: Acts as the root directory for deployment configurations, expected to be defined elsewhere in the system or inventory.Upgrade Logic: The playbook integrates with an upgrade mechanism via the
UPGRADEvariable, allowing selective configuration refreshes.FAST_UPDATE Flag: Controls whether a minimal update is performed versus a full configuration regeneration.
File Outputs: Generated files (e.g.,
docker-compose.yaml,aerospike.conf,logrotate.sh) are crucial for orchestrating containers, configuring the Aerospike database, and managing log rotation, respectively.
Visual Diagram: Flowchart of Main Tasks and Their Conditional Execution
flowchart TD
A[Check bk_set.json Exists] --> B[Set HAS_BK_SET_FILE Fact]
B --> C{Compose Generation?}
C -->|Yes| D[Generate docker-compose.yaml]
C -->|No| E[Skip Compose]
B --> F{Aerospike Config?}
F -->|Yes| G[Generate aerospike.conf]
F -->|No| H[Skip Aerospike]
B --> I{Logrotate Config?}
I -->|Yes| J[Generate logrotate.sh]
I -->|No| K[Skip Logrotate]
%% Conditions
C:::cond
F:::cond
I:::cond
classDef cond fill:#f9f,stroke:#333,stroke-width:2px
This flowchart illustrates the sequence starting from the file existence check, setting facts, and then branching into conditional generation of the three configuration files based on flags and upgrade conditions.