Skip to content

Instantly share code, notes, and snippets.

View mgedmin's full-sized avatar

Marius Gedminas mgedmin

View GitHub Profile
@mgedmin
mgedmin / filter_plugins__dns.py
Created September 4, 2024 11:24
Managing DNS servers with Ansible
# filter_plugins/dns.py
import dns.zone # pip install dnspython or pipx inject ansible dnspython
import dns.resolver
import dns.flags
def get_dns_serial(zone, ns=None):
resolver = dns.resolver.Resolver(configure=True)
resolver.use_edns(0, ednsflags=dns.flags.DO, payload=4096)
if ns:
@mgedmin
mgedmin / lock_user.py
Created July 4, 2024 10:17
Ansible module to lock a user account
#!/usr/bin/python
import spwd
import subprocess
DOCUMENTATION = '''
---
module: lock_user
short_description: locks user accounts
description:
- The M(lock_user) module invokes C(passwd -l) to lock user accounts.
@mgedmin
mgedmin / termtoggle.vim
Created April 26, 2024 11:23
Show/hide terminal by pressing F12 in Vim
fun! ToggleTerminal()
let terms = term_list()
if terms == [] " no terminals, open one
botright term
elseif bufwinnr(terms[0]) == -1 " terminal hidden, show it
exec 'botright sbuffer' terms[0]
else " terminal visible, hide it
exec bufwinnr(terms[0]) .. 'hide'
endif
endf
@mgedmin
mgedmin / day24.rs
Last active December 24, 2022 13:54
Advent of Code 2022, day 24 solution
use num_integer::lcm;
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap};
#[derive(Copy, Clone)]
pub struct Pos {
x: usize,
y: usize,
}
@mgedmin
mgedmin / day22.rs
Created December 22, 2022 15:51
Advent of Code 2022, day 22 solution
use lazy_static::lazy_static;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use num_integer::gcd;
use regex::Regex;
use std::collections::{HashMap, VecDeque};
#[repr(u8)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Facing {
Right = 0,
@mgedmin
mgedmin / part1.rs
Created December 21, 2022 08:35
Advent of Code 2022 solution for day 21
use std::collections::HashMap;
use num_rational::Rational64;
pub type Number = Rational64;
#[derive(Debug)]
pub enum Expr<'a> {
Unknown,
Number(Number),
Add(&'a str, &'a str),
@mgedmin
mgedmin / letsencrypt.yml
Created June 14, 2021 08:21
Example of an Ansible playbook that uses group_by
---
- hosts: all
tasks:
- group_by: key="{{ 'using_letsencrypt' if letsencrypt_certs|default([]) else 'not_using_letsencrypt' }}"
changed_when: no
tags: always
- group_by: key="{{ ansible_distribution.lower() ~ '_' ~ ansible_distribution_release }}"
changed_when: no
tags: always
import os
from units.compat import mock
from units.compat import unittest
from ansible.modules import apt_key
class AptKeyTestCase(unittest.TestCase):
@mgedmin
mgedmin / prune_libvirt_images.py
Created June 9, 2021 10:40
prune unused vagrant-libvirt images
#!/usr/bin/python3
"""
Script to remove unused libvirt images left lying around by libvirt-vagrant.
This is a workaround for
https://github.com/vagrant-libvirt/vagrant-libvirt/issues/85
"""
import argparse
import functools
@mgedmin
mgedmin / conftest.py
Created September 9, 2020 11:28
How to get a pytest progress bar in xterm title
# Put this in your top-level conftest.py
class XTermProgress:
def __init__(self, stdout=sys.stdout):
self.init(stdout)
self.session = None
self.reported = set()
self.args = []