Friday, July 15, 2016

Stupid Ansible Tricks

My new job requires that I become proficient in Ansible, quickly.

There's just 2 problems: I'm not very proficient in Ansible yet, and I'm not a huge fan of ansible in part because of the pain it caused in my last job.  In fact, the more I learn, the less of a fan I become. Everything feels clunky, overly complex, and generally wrong...



I'm hoping the curve will end up looking something like this:


In other words, I pray that as I come to understand it better, I'll see that I have preconceptions that are prejudicing my opinion, and that the reality is that I've been wrong all along.

I really hope that I'm currently right near the bottom of that curve and that I'm going to become an Ansible evangelist Any Moment Now:



Alas, I've got a sneaking suspicion that won't be the case:

In fact, I think the curve isn't even the right shape:




So with that rant out of the way, here's a few things I've learned:

1) ansible is not the same thing as ansible-playbook. ansible-playbook seems to be a tool for running playboook .yml files that declare how roles, inventories and other playbook .yml files define your infrastructure.

2) To ask ansible to run a command on all nodes in a certain group:

$ ansible -i {inventory} {group} -m shell -a "{command}"

3) To add a crontab entry that runs at 11:30pm every day via ansible, put this in roles/{role}/tasks/main.yml :

- name: {some name}
  cron: user="root" name="cron repair" state=present minute="30" hour="23" day="*" job="{path-to-script}"
  tags:
    - {tag1}
    - {tag2}

4) Run a shell command against a glob-selected subset of nodes:

$ ansible -i {inventory} {group} -l "{prefix}*" -m shell -a "{command}"

5) List hosts matching given pattern:

$ ansible {pattern} --list-hosts


... Expect more stupid ansible tricks to appear here ...