Skip to content

Instantly share code, notes, and snippets.

@alces
alces / ansible_local_playbooks.md
Last active April 5, 2024 18:28
How to run an Ansible playbook locally
  • using Ansible command line:
ansible-playbook --connection=local 127.0.0.1 playbook.yml
  • using inventory:
127.0.0.1 ansible_connection=local
@alces
alces / readYaml.groovy
Created May 2, 2017 13:04
Reading YAML from Groovy using snakeyaml (its jar file is available on Maven Central, no dependencies)
import org.yaml.snakeyaml.Yaml
new Yaml().load(new FileReader('path/to/your/yaml/file'))
// and do whatenver your want with the returned map
@alces
alces / sudo_env_vars.md
Last active July 21, 2023 09:21
How to change environment passed to sudo command

By default, sudo on the Linux systems executes commands into a minimal environment. So, if you for example has some non-standard directories (such as /opt/groovy/bin) added to your PATH, a command running via sudo won't see any executable in these directories.

Normally, this strict sudo behavior can be changed by removing env_reset (or changing env_keep) variables in /etc/sudoers. But what can you do in case when you have only restricted sudo without write access to this file?

The following command passes a current value of PATH under a command run with sudo privileges:

sudo bash -c "export $PATH; which groovy"
@alces
alces / k8s_nifi_grep.sh
Created June 30, 2023 14:01
Looking for a pattern inside NiFi flowfiles on multiple clusters deployed atop of k8s
#!/bin/bash
PATTERN='for-example-a-kafka-topic-name-to-look-for'
for ns in $(kubectl get ns | awk '{if ($1 !~ /^NAME|kube-/) {print $1}}')
do
echo "Looking into $ns"
kubectl exec -n "$ns" -c nifi nifi-0 -- zgrep "$PATTERN" permanent_configs/flow.xml.gz 2>/dev/null
done
@alces
alces / Jenkinsfile
Created October 7, 2019 06:16
Ask for password in Jenkins pipeline
pipeline {
agent {
label 'Linux'
}
stages {
stage('ask') {
steps {
script {
def askpass = input(
message: 'Please enter the password',
@alces
alces / mk_ansible_distro.sh
Last active May 31, 2023 17:43
Make portable Ansible distribution which can be installed on another host with the same Python version simply by un-tarring it in the same directory. Virtualenv is used during build stage, but isn't required on the target machine. Originally, written as Vagrant provision script but probably can be used outside Vagrant.
#!/bin/bash
ANSIBLE_ROOT=${ANSIBLE_ROOT:-/opt/ansible}
DISTR_ROOT=${DISTR_ROOT:-/vagrant}
yum install -y python-setuptools python-devel libffi-devel openssl-devel
easy_install virtualenv
virtualenv "$ANSIBLE_ROOT"
. "$ANSIBLE_ROOT/bin/activate"
# PY_LIBS can contain a space-separated list of Python libraries your playbooks require to run (e.g., "dnspython shade")
@alces
alces / fieldalignment_fix.sh
Created December 30, 2022 14:12
Fixing fieldalignment issues reported by govet
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment
fieldalignment -fix a_problematic_file.go
@alces
alces / jenkinsfile_pr_check.md
Created September 26, 2018 06:35
Using Jenkins pipelines as Github pull request cheks
  1. in Jenkins set up a multibranch pipeline with Branch source of type Github (under it, set up endpoint, credentials, repo name, etc.);
  2. in Github go to the repository Settings and add the user chosen on the previous step to the repository's colaborators;
  3. go to the Hooks menu and add a webhook pointing to <your-jenkins-host>/github-webhook/ and select pull request event under Let me select individual events option;
  4. create a pull request - after that Jenkins should automatically start a build;
  5. go to Branches menu under Settings and add the target branch to Protected branches;
  6. choose Require status checks to pass before merging and continuous-integration/jenkins/pr-merge under it
  7. commit a change into the pull request and see the Jenkins build result on the page.
@alces
alces / listLdapGroupsJNDI.groovy
Last active January 20, 2023 07:45
A groovy script returning a list of users' groups from LDAP server using JNDI API
import javax.naming.directory.*
MYDOM = 'example.com'
// convert DNS domain to a LDAP notation
dns2ldap = {dom ->
'DC=' + dom.split(/\./).join(',DC=')
}
// base OU for our search
@alces
alces / install_terraform_provider.sh
Created December 7, 2022 13:21
Install a Terraform provider to a cache (tested on alpine Docker image)
#!/bin/sh -e
# Install a Terraform provider
OS=linux
ARCH=amd64
OPTS=`getopt -l dont-unzip,full-url:,group:,provider:,site:,url:,version: -- df:g:p:u:s:v: "$@"` || exit 1
eval set -- "$OPTS"