3
0
forked from jakub/ansible

Compare commits

20 Commits
main ... main

Author SHA1 Message Date
f4262bcb27 Update test_sms.yml 2025-10-14 12:49:40 +00:00
5c69d3a03f Update test_sms.yml 2025-10-14 12:35:33 +00:00
547c9fadc5 Update test_sms.yml 2025-10-14 11:57:23 +00:00
c07181291c Update test_sms.yml 2025-10-14 11:55:48 +00:00
1a0ce36efe Update test_sms.yml 2025-10-13 12:39:59 +00:00
8b57f27ec6 Update test_sms.yml 2025-10-13 09:56:02 +00:00
085e7177f4 Update test_sms.yml 2025-10-13 09:47:28 +00:00
3099a0b2b8 Update test_sms.yml 2025-10-13 09:45:40 +00:00
3d89bc523e Update test_sms.yml 2025-10-13 09:43:52 +00:00
61d288f92a Update test_sms.yml 2025-10-12 19:00:20 +00:00
61beedd023 Update test_sms.yml 2025-10-12 18:59:20 +00:00
bb37cdaa53 Update test_sms.yml 2025-10-12 18:58:25 +00:00
b805b506b4 Update test_sms.yml 2025-10-12 18:56:33 +00:00
9fad4e4d1a Update inv_linuxes 2025-10-12 18:53:08 +00:00
a632da2a62 Update test_sms.yml 2025-10-12 18:50:51 +00:00
cf21ad70c1 Add test_sms.yml 2025-10-12 18:45:30 +00:00
1deb268d73 Update inv_mikrotiks 2025-09-20 10:26:36 +00:00
8373252ae9 Update update.yml 2025-09-20 10:12:50 +00:00
13a48cd734 Update inv_mikrotiks 2025-09-19 11:20:04 +00:00
b497723769 Update mikrotikbackup.yml 2025-09-19 11:13:44 +00:00
5 changed files with 210 additions and 18 deletions

View File

@@ -2,4 +2,7 @@
jimbuntu ansible_host=192.168.19.4
jim_storage ansible_host=192.168.19.7
portainer2 ansible_host=192.168.52.9
portainernode ansible_host=192.168.52.21
portainernode ansible_host=192.168.52.21
[local]
localhost ansible_connection=local

View File

@@ -1,2 +1,3 @@
[mikrotiks]
storage ansible_host=192.168.19.3 ansible_ssh_common_args='-o StrictHostKeyChecking=no'
jim ansible_host=192.168.19.2
hellsos ansible_host=192.168.40.1

View File

@@ -7,7 +7,7 @@
- name: Ensure output directory exists
ansible.builtin.file:
path: output
path: /opt/mikrotik_backups
state: directory
mode: '0755'
delegate_to: localhost
@@ -42,7 +42,7 @@
- name: Save export to local file
ansible.builtin.copy:
content: "{{ export_output.stdout }}"
dest: "output/{{ router_name }}-{{ current_date }}.config"
dest: "/opt/mikrotik_backups/{{ router_name }}-{{ current_date }}.config"
delegate_to: localhost
when: export_output.rc == 0
@@ -52,11 +52,11 @@
when: system_identity.rc == 0
- name: Download binary backup
shell: timeout 15 scp -o StrictHostKeyChecking=no -P {{ ansible_port }} {{ ansible_user }}@{{ ansible_host }}:{{ router_name }}-{{ current_date }}-backup.backup output/
shell: timeout 15 scp -o StrictHostKeyChecking=no -P {{ ansible_port }} {{ ansible_user }}@{{ ansible_host }}:{{ router_name }}-{{ current_date }}-backup.backup /opt/mikrotik_backups/
delegate_to: localhost
when: system_identity.rc == 0
- name: Remove backup file from router
shell: timeout 15 ssh -o StrictHostKeyChecking=no {{ ansible_user }}@{{ ansible_host }} -p {{ ansible_port }} "/file remove {{ router_name }}-{{ current_date }}-backup.backup"
delegate_to: localhost
when: system_identity.rc == 0
when: system_identity.rc == 0

57
test_sms.yml Normal file
View File

@@ -0,0 +1,57 @@
---
- name: Send and verify SMS delivery via internet-master.cz
hosts: localhost
gather_facts: false
vars:
sms_number: "601358865"
sms_username: "mikrotik"
sms_password_send: "jdkotzHJIOPWhjtr32D"
sms_password_recv: "jdkotzHJIOPWhjtr32D"
sms_wait_seconds: 120 # Wait 2 minutes for delivery
tasks:
- name: Generate random test string
set_fact:
random_string: "mikrotik_{{ lookup('password', '/dev/null length=8 chars=ascii_letters') }}"
- name: Send SMS message
uri:
url: "https://sms.internet-master.cz/send/?number={{ sms_number }}&message=@mikrotik@{{ random_string | urlencode }}&type=class-1&username={{ sms_username }}&password={{ sms_password_send }}"
method: GET
return_content: true
register: send_result
- name: Show send API response
debug:
var: send_result.content
- name: Wait for SMS to be delivered
pause:
seconds: "{{ sms_wait_seconds }}"
- name: Fetch received messages
uri:
url: "https://sms.internet-master.cz/receive/?username={{ sms_username }}&password={{ sms_password_recv }}"
method: GET
return_content: true
register: recv_result
- name: Parse received JSON
set_fact:
inbox: "{{ recv_result.json.inbox | default([]) }}"
- name: Check if random string message was received
set_fact:
message_found: "{{ inbox | selectattr('message', 'equalto', random_string) | list | length > 0 }}"
- name: Report result
debug:
msg: >
SMS with message '{{ random_string }}' was {{
'delivered ✅' if message_found else 'NOT delivered ❌'
}}.
- name: Fail if not delivered
fail:
msg: "Message '{{ random_string }}' not found in received inbox!"
when: not message_found

View File

@@ -1,34 +1,165 @@
- name: Update system (APT + Flatpak)
hosts: all
gather_facts: false
strategy: free
serial: 2
become: true
become_user: root
become_method: sudo
vars:
ssh_precheck_timeout: 8
apt_async: 1800
apt_poll: 10
apt_retries: 3
apt_retry_delay: 5
flatpak_timeout: 300
flatpak_async: 600
flatpak_poll: 5
pre_tasks:
- name: Ensure SSH is reachable (skip host if not)
wait_for:
host: "{{ ansible_host | default(inventory_hostname) }}"
port: "{{ ansible_port | default(22) }}"
timeout: "{{ ssh_precheck_timeout }}"
delegate_to: localhost
register: ssh_ok
ignore_errors: true
- meta: end_host
when: ssh_ok is failed
- name: Ping with retries (handle intermittent flaps)
ping:
register: ping_r
retries: 3
delay: 3
until: ping_r is succeeded
ignore_errors: true
- meta: end_host
when: ping_r is failed
tasks:
- name: Update APT cache
- name: Update APT cache (bounded + retried)
environment: { DEBIAN_FRONTEND: noninteractive }
apt:
update_cache: yes
cache_valid_time: 3600
async: "{{ apt_async }}"
poll: "{{ apt_poll }}"
register: apt_update
retries: "{{ apt_retries }}"
delay: "{{ apt_retry_delay }}"
until: apt_update is succeeded
- name: Upgrade all APT packages
- name: If APT cache update failed, try to fix dpkg and retry once
block:
- name: Fix partially configured packages
command: dpkg --configure -a
changed_when: false
- name: Retry APT cache update after dpkg fix
environment: { DEBIAN_FRONTEND: noninteractive }
apt:
update_cache: yes
async: 600
poll: 5
when: apt_update is failed
- name: Upgrade all APT packages (bounded + retried)
environment: { DEBIAN_FRONTEND: noninteractive }
apt:
upgrade: dist
async: "{{ apt_async }}"
poll: "{{ apt_poll }}"
register: apt_upgrade
retries: "{{ apt_retries }}"
delay: "{{ apt_retry_delay }}"
until: apt_upgrade is succeeded
- name: If APT upgrade failed, try to fix dpkg and retry once
block:
- name: Fix partially configured packages
command: dpkg --configure -a
changed_when: false
- name: Retry APT upgrade after dpkg fix
environment: { DEBIAN_FRONTEND: noninteractive }
apt:
upgrade: dist
async: 1200
poll: 5
when: apt_upgrade is failed
- name: Check if flatpak binary exists
become: false
stat:
path: /usr/bin/flatpak
register: flatpak_bin
- name: Update system Flatpaks
shell: timeout 300 flatpak update -y
- name: Update system Flatpaks (bounded; treat timeout as non-fatal)
command: bash -lc "timeout {{ flatpak_timeout }} flatpak update -y --noninteractive"
register: flatpak_sys
failed_when: flatpak_sys.rc != 0 and flatpak_sys.rc != 124
async: "{{ flatpak_async }}"
poll: "{{ flatpak_poll }}"
failed_when: flatpak_sys.rc is defined and flatpak_sys.rc not in [0, 124]
when: flatpak_bin.stat.exists
- name: Update user Flatpaks
become_user: jakub
environment:
XDG_RUNTIME_DIR: /run/user/1000
shell: timeout 300 flatpak update -y
register: flatpak_user
failed_when: flatpak_user.rc != 0 and flatpak_user.rc != 124
# ---- User-agnostic Flatpak updates (all non-system users) ----
- name: Get passwd database
getent:
database: passwd
register: ge
- name: Build list of regular users (uid >= 1000, real shells)
set_fact:
regular_users: >-
{{
ge.ansible_facts.getent_passwd
| dict2items
| map(attribute='value')
| selectattr('uid', 'defined')
| selectattr('uid', '>=', 1000)
| rejectattr('shell', 'in', ['/usr/sbin/nologin','/sbin/nologin','/bin/false'])
| list
}}
when: ge is succeeded
- name: Stat per-user runtime dir if flatpak is present
stat:
path: "/run/user/{{ item.uid }}"
loop: "{{ regular_users | default([]) }}"
loop_control:
label: "{{ item.name }}"
register: user_runtime_stats
when: flatpak_bin.stat.exists
- name: Merge runtime stats keyed by username
set_fact:
user_runtime_map: >-
{{
user_runtime_stats.results
| items2dict(key_name='item.name', value_name='stat')
}}
when: flatpak_bin.stat.exists
- name: Update user Flatpaks (use XDG_RUNTIME_DIR when available)
become_user: "{{ item.name }}"
environment: >-
{{
user_runtime_map[item.name].exists
| default(false)
| ternary({'XDG_RUNTIME_DIR': '/run/user/' ~ item.uid|string}, {})
}}
command: bash -lc "timeout {{ flatpak_timeout }} flatpak --user update -y --noninteractive"
register: flatpak_user_res
async: "{{ flatpak_async }}"
poll: "{{ flatpak_poll }}"
failed_when: flatpak_user_res.rc is defined and flatpak_user_res.rc not in [0, 124]
changed_when: "'Installing' in (flatpak_user_res.stdout | default('')) or 'Installing' in (flatpak_user_res.stderr | default('')) or 'Updating' in (flatpak_user_res.stdout | default('')) or 'Updating' in (flatpak_user_res.stderr | default(''))"
loop: "{{ regular_users | default([]) }}"
loop_control:
label: "{{ item.name }}"
when: flatpak_bin.stat.exists