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:51] manu |
ansible:ansible_sandbox [2025/05/28 10:28] (current) manu [Test 4 insertafter using bash] |
||
---|---|---|---|
Line 136: | Line 136: | ||
mail_pw: PASSWORD_HERE #password for the mail_from mail address | mail_pw: PASSWORD_HERE #password for the mail_from mail address | ||
mail_port: 587 #the port where mails are sent to the mail server, e.g. 587 | mail_port: 587 #the port where mails are sent to the mail server, e.g. 587 | ||
+ | </code> | ||
+ | |||
+ | cat group_vars/all.yml | ||
+ | <code> | ||
+ | --- | ||
+ | # general settings | ||
+ | default_username: debian | ||
+ | dot_forward_email: <YOUR_EMAIL_GOES_HERE> | ||
+ | private_key: .ssh/id_rsa | ||
+ | public_key: .ssh/id_rsa.pub | ||
+ | ntpserver: pool.ntp.org | ||
+ | timezone: Europe/Rome | ||
+ | |||
+ | # default sshd port | ||
+ | sshd_port: 22 | ||
+ | |||
+ | # generate random passwords for default user and root user | ||
+ | default_password: "{{lookup('password', '/dev/null length=15 chars=ascii_letters,digits,punctuation')}}" | ||
+ | root_password: "{{lookup('password', '/dev/null length=15 chars=ascii_letters,digits,punctuation')}}" | ||
+ | |||
+ | # unattended packages install configuration | ||
+ | unattended_mail: "{{dot_forward_email}}" | ||
+ | unattended_remove_unused_dependencies: true | ||
+ | unattended_automatic_reboot_time: "03:00" | ||
+ | unattended_update_days: "Sat" | ||
+ | unattended_clean_interval: 7 | ||
+ | |||
+ | # fail2ban | ||
+ | fail2ban_loglevel: INFO | ||
+ | fail2ban_services: | ||
+ | - name: ssh | ||
+ | port: ssh | ||
+ | filter: sshd | ||
+ | logpath: /var/log/auth.log | ||
</code> | </code> | ||
Line 259: | 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 | ||
+ | |||
+ |