There are so many PCs in my house, and they are becoming a pain to keep up to date and consistent. Recently I’ve been writing Ansible scripts to automate all the configuration I used to do by hand. It’s been my new season’s resolution not to do any more manual configuration, especially for my home PCs.
Ansible makes installing software, SSH keys and setting up user accounts really easy.
It can even configure some aspects of better-behaved packages. For example, here’s my Ansible playbook for installing Bittorrent Sync – a fabulous alternative to rsync.
- hosts: btsync sudo: True tasks: - name: Add repository apt_repository: repo='ppa:tuxpoldo/btsync' update_cache=yes state=present - name: Install packages apt: pkg={{item}} state=installed with_items: - btsync - debconf-utils - btsync-gui - transmission-daemon - name: Configure BT Sync debconf: name='btsync' question='btsync/runas' value='sal' vtype='string' debconf: name='btsync' question='btsync/directory_root' value='/home/sal' vtype='string' debconf: name='btsync' question='btsync/webgui-bindaddr' value='0.0.0.0' vtype='string' debconf: name='btsync' question='btsync/folder_defaults-use_lan_broadcast' value='true' vtype='boolean' debconf: name='btsync' question='btsync/log_size' value='1' vtype='string' debconf: name='btsync' question='btsync/folder_defaults-use_dht' value='true' vtype='boolean' - name: Regenerate BT Sync's config file command: dpkg-reconfigure -f noninteractive btsync - name: Restart BT Sync service service: name='btsync' state=restarted
First of all it ensures that the PPA containing the Bittorrent Sync software is installed. Setting update_cache to “yes” is the equivalent of an apt-get update.
Next it installs a bunch of packages required to run and configure the applications I need. Ansible provides an interface to allow me to set debconf variables but does not seem to provide anthing to recompile the configuration, hence I have to remotely execute the dpkg-reconfigure command.
Once it’s all done I can simply restart the service.
To get all this working easily I need a few extra things. First of all, a hosts file – that defines the group of hosts that I want to install this package to. I’ve defined a group of hosts called “btsync” – these are all of the machines I want to install the Bittorrent software onto.
[btsync] bobnit ahfang tinyenid halob
It’s just a list of host-names in an ini-style text-file.
And I also need a handy way to run it. To save me from remembering the command-line syntax I wrote a small shall-script:
#!/usr/bin/env bash source venv/bin/activate export ANSIBLE_HOSTS=./hosts ansible-playbook -s --ask-sudo-pass --ask-pass btsync.yml
The -s option forces everything to run as if from a sudo command. The –ask-sudo-pass and –ask-pass command line arguments make the playbook-runner ask the user for passwords before starting. That saves me from having to store any passwords in any of the configuration files.
Running the script should produce an output a little like this: