Lampros - Weird Bricks

Bootstrapping pkg and installing Python on a FreeBSD target via Ansible

23 June, 2014 | Ansible

This post is a sequel of this one.

This time, I've edited the Ansible hosts (/usr/local/etc/ansible/hosts) and instead of targetting my Ubuntu 14 LTS box I am targetting my FreeBSD 10 one.

Like before I've copied the ssh keys and I'll try to ping it with Ansible:

ansible all -m ping -u root

Output:

192.168.2.16 | FAILED >> {
    "failed": true,
    "msg": "/usr/local/bin/python: not foundrn",
    "parsed": false
}

OK, so once again Python is giving us trouble - so let's see where Python is installed on the target.

ssh root@192.168.2.16 which python

Output:

python: Command not found.

Oh boy, no Python. What do we do now? We could just use a regular SSH command and install Python but that's cheating - we want Ansible to be able to do this.

Enter the 'raw' module

From the documentation:

Executes a low-down and dirty SSH command, not going through the module subsystem.

Sounds good! Let's use this and try installing Python:

ansible all -m raw -a 'pkg install python'

*crickets* ... nothing happens, it just hanged there, so I ctrl-c to stop this.

Went to the FreeBSD host and tried the same command locally:

pkg install python
The package management tool is not yet installed on your system.
Do you want to fetch and install it now? [y/N]:

Aha - so there's a prompt it's getting stuck on. Let's try to pipe yes to it over ansible and see if that does it:

ansible all -m raw -a 'yes | pkg info'

Output:

192.168.2.16 | FAILED | rc=1 >>
The package management tool is not yet installed on your system.
Do you want to fetch and install it now? [y/N]:

FAILED?

After lots of googling I found this: https://dan.langille.org/2013/12/06/bootstrapping-installing-pkg-on-freebsd-unattended-and-without-answering-yes/

And tried the following:

ansible all -m raw -a 'env ASSUME_ALWAYS_YES=YES pkg bootstrap'

Output:

ansible all -m raw -a 'env ASSUME_ALWAYS_YES=YES pkg bootstrap'
192.168.2.16 | success | rc=0 >>
Bootstrapping pkg from pkg+http://pkg.FreeBSD.org/freebsd:10:x86:32/latest, please wait...
Verifying signature with trusted certificate pkg.freebsd.org.2013102301... done
Installing pkg-1.2.7_3... done
If you are upgrading from the old package format, first run:

  # pkg2ng

Nice.

Now let's modify this slightly to do both the pkg bootstrap and python installation in one step:

ansible all -m raw -a 'env ASSUME_ALWAYS_YES=YES pkg bootstrap -f; env ASSUME_ALWAYS_YES=YES pkg install python'

Output:

192.168.2.16 | success | rc=0 >>
Bootstrapping pkg from pkg+http://pkg.FreeBSD.org/freebsd:10:x86:32/latest, please wait...
Verifying signature with trusted certificate pkg.freebsd.org.2013102301... done
Installing pkg-1.2.7_3...pkg-static: package pkg is already installed, forced install
 done
If you are upgrading from the old package format, first run:

  # pkg2ng
Updating repository catalogue
The following 3 packages will be installed:

        Installing python27: 2.7.6_4
        Installing python2: 2_2
        Installing python: 2.7_1,2

The installation will require 66 MB more space

0 B to be downloaded
Checking integrity... done
[1/3] Installing python27-2.7.6_4... done
[2/3] Installing python2-2_2... done
[3/3] Installing python-2.7_1,2... done
====
Note that some of the standard modules are provided as separate
ports since they require extra dependencies:

bsddb           databases/py-bsddb
gdbm            databases/py-gdbm
sqlite3         databases/py-sqlite3
tkinter         x11-toolkits/py-tkinter

Install them as needed.
====

Nice.

Now let's try the ping again:

ansible all -m ping -u root

Output:

192.168.2.16 | success >> {
    "changed": false,
    "ping": "pong"
}

Epic.