This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
ansible:ansible_sandbox [2025/02/21 16:55] manu [Test 2 with roles] |
ansible:ansible_sandbox [2025/05/28 10:28] (current) manu [Test 4 insertafter using bash] |
||
|---|---|---|---|
| Line 293: | Line 293: | ||
| - nmap-ncat | - nmap-ncat | ||
| </code> | </code> | ||
| + | |||
| + | ===== Test 3 backup file ===== | ||
| + | |||
| + | |||
| + | | ||
| + | File is backuped with format **2025-02-23T14:30:00Z** | ||
| + | <cli prompt='#'> | ||
| + | --- | ||
| + | - name: Backup file if it exists | ||
| + | hosts: localhost | ||
| + | tasks: | ||
| + | - name: Check if the file exists | ||
| + | stat: | ||
| + | path: /path/to/your/file | ||
| + | register: file_stat | ||
| + | |||
| + | - name: Create a backup if the file exists | ||
| + | copy: | ||
| + | src: /path/to/your/file | ||
| + | dest: "/path/to/backup/file_{{ ansible_date_time.iso8601 }}.bak" | ||
| + | remote_src: yes | ||
| + | when: file_stat.stat.exists | ||
| + | </cli> | ||
| + | |||
| + | Backup only if modified | ||
| + | <cli prompt='#'> | ||
| + | --- | ||
| + | - name: Backup file if it is different | ||
| + | hosts: localhost | ||
| + | tasks: | ||
| + | - name: Get the checksum of the current file | ||
| + | stat: | ||
| + | path: /path/to/your/file | ||
| + | register: file_stat | ||
| + | |||
| + | - name: Get the checksum of the last backup (if exists) | ||
| + | stat: | ||
| + | path: "/path/to/backup/file_last.bak" | ||
| + | register: backup_stat | ||
| + | ignore_errors: yes | ||
| + | |||
| + | - name: Compare the current file checksum with the backup checksum | ||
| + | command: "sha256sum /path/to/your/file | awk '{ print $1 }'" | ||
| + | register: current_checksum | ||
| + | when: file_stat.stat.exists | ||
| + | |||
| + | - name: Compare the backup checksum (if backup exists) | ||
| + | command: "sha256sum /path/to/backup/file_last.bak | awk '{ print $1 }'" | ||
| + | register: backup_checksum | ||
| + | when: backup_stat.stat.exists | ||
| + | |||
| + | - name: Backup the file if checksums are different | ||
| + | copy: | ||
| + | src: /path/to/your/file | ||
| + | dest: "/path/to/backup/file_{{ ansible_date_time.iso8601 }}.bak" | ||
| + | remote_src: yes | ||
| + | when: | ||
| + | - file_stat.stat.exists | ||
| + | - (backup_stat.stat.exists == false or current_checksum.stdout != backup_checksum.stdout) | ||
| + | </cli> | ||
| + | |||
| + | ===== Test 4 insertafter using bash ===== | ||
| + | |||
| + | <cli> | ||
| + | --- | ||
| + | - name: Insert line after pattern using bash | ||
| + | hosts: all | ||
| + | become: true | ||
| + | tasks: | ||
| + | - name: Insert line after pattern using sed | ||
| + | shell: | | ||
| + | if ! grep -q '^new_config_line=value$' /etc/example.conf; then | ||
| + | sed -i '/# INSERT HERE/a new_config_line=value' /etc/example.conf | ||
| + | fi | ||
| + | args: | ||
| + | executable: /bin/bash | ||
| + | </cli> | ||
| + | |||
| + | https://stackoverflow.com/questions/70162334/in-ansible-how-do-i-add-a-line-without-delete-comment | ||
| + | |||
| + | https://www.theunixschool.com/2012/06/insert-line-before-or-after-pattern.html | ||
| + | |||
| + | |||