Using Ansible to Compile NGINX from Sources with Custom Modules

Daniel Korn
BigPanda Engineering
3 min readOct 11, 2017

--

In my previous post, I explained how to set up a NGINX server with custom modules using Vagrant. As it’s often the case when working on Infra/DevOps tasks, once you’ve got the task done you’re likely going to repeat it, and that’s where automation enters the game.

There are various good configuration management tools out there. As you probably guessed from the title, I chose Ansible for three main reasons:

  1. Ansible is simple. while it is both powerful and secure, it doesn’t require any special coding skills and pretty much “human readable”. Also, all operations are executed by order and the outcome is predictable, as there aren’t any agents around to manage
  2. Ansible is heavily used in the startup I work for. We have a decent “Playbooks” repo, including all roles needed for our Infra and CI
  3. I’ve gained some experience using it in the past. I wrote several modules as a developer at Red Hat, gave talks at open source conferences about Ansible and had my share of playbooks runs.

Just to clarify: the purpose of this post is not to introduce Ansible. I’ll try and focus on the job mentioned while stressing only the “nontrivial” points.

Now, back to work.
Let’s start with short planning - what are the main building blocks when breaking this assignment into parts:

  • install the dependencies
  • download the sources
  • configure NGINX to include our custom modules
  • Install NGINX (compile it)

Clearly, there are more tasks to take care of, e.g. unpacking, creating dirs copying conf files and more, but all those are pretty common as you’ll see.
The most important thing we can conclude from the planning is that luckily for us, we don’t need to write any new Ansible Module. A simple Playbook will get the job done.

A few general explanations and disclaimers (more specific ones after the playbook gist):

  • this is a basic playbook. though many improvements and advanced configurations can be made, my top priority was to keep it simple
  • I added some sample variables at the top. It is a better practice to load the vars from separated included files, but again, I wanted to put as much content as possible in the playbook while keeping it as simple as possible
  • there are multiple configure options available with the ./configure script that sets up various NGINX parameters. I mentioned three: --sbin-path and --conf-path needed for nginx.init file;--with- for the custom module we want to include.
  • using NGINX as a service requires setting it in anginx.init file. I used a basic ubuntu one, several examples can be found here. In addition, you need to define the pid path. this can be done with the --pid-path= configure option or set in your nginx.conf

The Playbook

Comment and notes:

  • make sure to use the compatible package manager module for your Linux distribution (For example, yum for CentOS/Fedora). same goes for the init script if used
  • creating dirs and setting paths depends on the way you configure your NGINX and in no way forced

I hope this post and gist come in handy. Feel free to comment and suggest improvements.

Special thanks to Noa Kurman for her help with nginx.init script.

For (almost) daily “Today I Learned” coding tips n tricks checkout my website.

--

--