如何在Ansible中为3台实例配置错开3小时的Cron备份任务
Solution
We can implement this directly in Ansible using either dynamic variable calculation or static inventory offsets. Here are both practical approaches:
Approach 1: Dynamic Staggering Based on Group Position
This method automatically assigns staggered hours based on the server's position in your inventory group.
Step 1: Define Your Inventory Group
Ensure all three servers are in a dedicated group (e.g., backup_servers) in your inventory:
[backup_servers] box1.example.com box2.example.com box3.example.com
Step 2: Playbook Implementation
- name: Deploy backup script and staggered cron jobs hosts: backup_servers become: true vars: app_home_dir: "/opt/your_app" # Replace with your actual app directory tasks: # Deploy the backup script to all servers - name: Copy S3 backup script to target directory copy: src: "local/path/to/aws_s3_backup.sh" # Local path to your script dest: "{{ app_home_dir }}/tools/aws_s3_backup.sh" mode: '0755' # Ensure executable permissions # Calculate the server's 0-based index in the backup_servers group - name: Get server index in backup group set_fact: server_index: "{{ groups['backup_servers'].index(inventory_hostname)|int }}" # Map index to target mod value (0→1, 1→2, 2→0) - name: Determine hour mod condition set_fact: target_mod: "{{ [1,2,0][server_index] }}" # Generate comma-separated hour list matching the mod condition - name: Generate backup hours string set_fact: backup_hours: "{{ range(0,24)|select('int')|select('mod',3, target_mod)|list|join(',') }}" # Create the cron job with staggered hours - name: Create S3 Backup cron job cron: name: "S3 Backup" minute: "0" hour: "{{ backup_hours }}" job: "{{ app_home_dir }}/tools/aws_s3_backup.sh"
Approach 2: Static Offsets in Inventory
For more control (e.g., if inventory order might change), define static offsets directly in your inventory:
Step 1: Update Inventory with Offsets
[backup_servers] box1.example.com backup_offset=1 box2.example.com backup_offset=2 box3.example.com backup_offset=0
Step 2: Simplified Playbook
- name: Deploy backup script and staggered cron jobs hosts: backup_servers become: true vars: app_home_dir: "/opt/your_app" # Replace with your actual app directory tasks: # Deploy the backup script - name: Copy S3 backup script to target directory copy: src: "local/path/to/aws_s3_backup.sh" dest: "{{ app_home_dir }}/tools/aws_s3_backup.sh" mode: '0755' # Generate hours using the static offset from inventory - name: Generate backup hours string set_fact: backup_hours: "{{ range(0,24)|select('int')|select('mod',3, backup_offset)|list|join(',') }}" # Create the cron job - name: Create S3 Backup cron job cron: name: "S3 Backup" minute: "0" hour: "{{ backup_hours }}" job: "{{ app_home_dir }}/tools/aws_s3_backup.sh"
Key Notes
- Both approaches generate the exact hour patterns you specified:
- Box1:
1,4,7,10,13,16,19,22 - Box2:
2,5,8,11,14,17,20,23 - Box3:
0,3,6,9,12,15,18,21(cron uses0for midnight, which matches your00requirement)
- Box1:
- The
modfilter in Jinja2 selects hours wherehour % 3 == target_mod, which implements your desired logic. - Replace placeholder paths (like
app_home_dirand script source path) with your actual values.
内容的提问来源于stack exchange,提问作者darrensunley
相关产品推荐
相关产品推荐

