Skip to content

Instantly share code, notes, and snippets.

@mifix
Last active March 26, 2016 12:23
Show Gist options
  • Save mifix/463e4caa3956779a1d0a to your computer and use it in GitHub Desktop.
Save mifix/463e4caa3956779a1d0a to your computer and use it in GitHub Desktop.
Automatically add packages to ansible

My system is fully managed via ansible. After apt-get install new packages, I have to remember to add them to a certain ansible file.

Add the following script (ai.sh) to your shell rc file and install new packages with:

$ ai -y htop git

See the python script below for reference or standalone usage.

Remember to change fname.

#!/usr/bin/env python
import yaml
import sys
args = filter(lambda x: not x.startswith('-'), sys.argv[1:])
fname = 'autoinstall.yml'
k = 'autoinstall_packages'
cfg = yaml.load(open(fname, 'a+')) or {}
cfg.setdefault(k, [])
cfg[k] = list(set((cfg[k] or []) + args))
f = open(fname, 'w')
f.write(yaml.dump(cfg, default_flow_style = False))
function ai() { apt install ${*} && python -c "fname = 'unsorted_packages.yml'; k = 'unsorted_packages'; import yaml; args = '${*}'; args = filter(lambda x: not x.startswith('-'), args.split()); cfg = yaml.load(open(fname, 'a+')) or {}; cfg.setdefault(k, []); cfg[k] = list(set((cfg[k] or []) + args)); f = open(fname, 'w'); f.write(yaml.dump(cfg, default_flow_style = False))" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment