Skip to content

Instantly share code, notes, and snippets.

@alces
alces / jenkins_views_containing_job.md
Last active May 31, 2016 09:32
Getting a list of Jenkins views a given job is a member of

The following script implies that your Jenkins has Nested View Plugin installed. Without it, our task is can be solved as easy as that:

jnk = jenkins.model.Jenkins.instance
jnk.views.findAll {
  jnk.getItem(OUR_JOB_NAME) in it.items
}
@alces
alces / jenkins_view_add_rm_jobs.groovy
Created May 27, 2016 06:57
Adding a Jenkins Job to and Removing from a List View
myView = hudson.model.Hudson.instance.getView('My Favorite View')
// let's add a job by its name
myView.doAddJobToView('aGoodJob')
// and remove another
myView.doRemoveJobFromView('aBadJob')
@alces
alces / jenkins_rename_jobs.groovy
Created May 26, 2016 13:26
A a Suffix to the Names of Jenkins Jobs Matching a Given Regular Expression
hudson.model.Hudson.items.findAll {
it.name =~ /SOME_REGEXP/
}.each {
it.renameTo(it.name + '.old')
}
@alces
alces / ansible_ad_hoc_inventories.md
Last active September 2, 2022 07:17
Using Ad-hoc Inventories in Ansible

In case you want to run ansible (or ansible-playbook) command against a set of hosts that makes sense only for one run, you can don't bother to create one-time inventory file, but simply define a comma-separated list of hosts as argument of --invertory option (or its short form that's simply -i) as follows:

ansible --inventory=myhost1,myhost2,myhost3 all -m setup -a 'filter=*name*'

(note that all in this command line stands for the target hostname)

If you have only one host to run your playbook against, your inventory string must ends with ,

@alces
alces / ansible_without_tty.md
Created May 23, 2016 13:12
How to force Ansible to don't open TTY

In some environments, users used for applications deployment are restricted from creating intercative sessions. By default, in a session opened by Ansible SSH_TTY environment variable is set, so this session can seems interactive. A found workaround is to turn on Pipelinig mode in Ansible configuration:

[ssh_connection]
pipelining = True

As a free benefit, your tasks in such kind of configuration would run a bit faster.

@alces
alces / ansible_local_playbooks.md
Last active May 20, 2024 14:57
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 / 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 / simple_pythonic_daemon.md
Last active May 17, 2016 07:39
Start child process as a daemon in Python

1. Classical way

This style of coding should look familiar for the old Unix programmers, because it uses C-like functions from os module:

import os

pid = os.fork()
if pid:
  # in a parent process - write the child's pid
@alces
alces / docker_links.md
Last active May 13, 2016 10:32
Example of using linked Docker containers installed by Vagrant docker provisioner
@alces
alces / ansible_hosts_patterns.md
Last active April 7, 2020 17:46
Using hosts patterns in Ansible command line

How to Use Hosts Patterns for Executing Ad-hoc Commands by Ansible

The best-documented (and maybe simply the best) way of running Ansible commands against a set of target hosts is by using hosts' groups in inventory file. For example:

[git]
git-au-node1.example.com
git-au-node2.example.com
git-au-node3.example.com
git-us-node1.example.com