technology from back to front

Testing Ansible playbooks with Vagrant

One of the projects here at LShift uses ansible to configure its EC2 machines. I needed to refactor the playbook in a minor way. But of course “refactor” doesn’t just mean “change the code”. It means “change the code (presumably for the better) while preserving behaviour“. I really didn’t want to check the change by running the playbook against a production deployment! So what to do?

Handily, vagrant supports ansible, so I can run a playbook from scratch simply by saying vagrant up. I had to smooth out some wrinkles: EC2 Ubuntu machine have an ‘ubuntu’ user, while Vagrant Ubuntu boxes have a ‘vagrant’ user. EC2 Ubuntu machines have a /dev/xvdb device, Vagrant boxes don’t.

And thus, this little shim playbook came to be:

- hosts: target
  user
: vagrant
  sudo
: True

  tasks
:
    - name
: Create fake block device for /dev/xvdb, simulating ephemeral storage
      action
: file src=/dev/ram1 path=/dev/xvdb mode=0660 owner=root group=disk state=link

    - name
: Create fake block device for /dev/xvdf, simulating an EBS volume
      action
: file src=/dev/ram2 path=/dev/xvdf mode=0660 owner=root group=disk state=link

    # As yet there's no ansible module for making a file system [1]. We try, and if mkfs fails
    # we presume it's because there's already a file system on that device, and eat the
    # failure.
    #
    # [1] https://github.com/ansible/ansible/pull/2776
    - name
: Put a filesystem on block devices to emulate EBS volumes
      action
: shell mkfs -t ext4 /dev/$item ; true
      with_items
:
       - xvdb
        - xvdf

    - name
: Create the ubuntu user
      action
: user name=ubuntu state=present groups=admin

    - name
: Make the ubuntu user an sudoer
      action
: lineinfile dest=/etc/sudoers regexp="^ubuntu" line="ubuntu ALL=(ALL) ALL" state=present

    - name
: Make /home/ubuntu/.ssh/
      action
: file path=/home/ubuntu/.ssh/ state=directory

    # This is not as lame as it looks. The real playbook will run as the 'ubuntu' user,
    # and this is a test environment.
    - name
: Copy the ansible key from the vagrant user to the ubuntu user
      action
: shell cp /home/vagrant/.ssh/authorized_keys /home/ubuntu/.ssh/authorized_keys creates=/home/ubuntu/.ssh/authorized_keys

    - name
: Fix permissions of same
      action
: file path=/home/ubuntu/.ssh/ owner=ubuntu group=ubuntu mode=0600 state=directory
      action
: file path=/home/ubuntu/.ssh/authorized_keys owner=ubuntu group=ubuntu mode=0600 state=file

- include
: therealplaybook.yml
by
Frank Shearar
on
15/05/13
 
 


× eight = 56

2000-14 LShift Ltd, 1st Floor, Hoxton Point, 6 Rufus Street, London, N1 6PE, UK+44 (0)20 7729 7060   Contact us