Using Ansible to Deploy Website from a git Repo

Перевод: Деплоим сайт из git резозитория GitHub с помощью Ansible

Ansible playbook with Red Hat tag RedHat Ansible

One of my old hosting setups is showing its age and becoming awkward to maintain. I have migrated as much as I could into my WPengine account, but a few websites still need to be migrated and I plan on hosting them with Docker containers. Ansible automation all the way.

Why Tracking Website Content in git is Cool

With website hosting, there’s always some trickery involved in maintaining content. You should consider yourself lucky if you are doing it with source control solution like git – it really helps not only do things in an orderly manner but also scale them to larger team supporting website.

Even if you believe you’ll “just install this once” and then “mostly focus on content”, blogging software would have to be maintained – meaning there will be upgrade times and there will be rollback times. So source control is key to going through such transitions smoothly enough.

I decided to store these old websites in GitHub: it’s a convenient approach I’m increasingly reliant on, it scales well and it gives me versioned control of everything. But this time it’s also giving me a distribution method: instead of downloading code onto my laptop and then somehow transferring latest copy to the server, I can just checkout GitHub repo onto the server directly.

Ansible has a great little module for doing this: git module. It provides much more flexibility that I need right now, all in an intrinsically Ansible easy-to-read way.

How to Pull a Git Repo in Ansible

name: Deploy website code from GitHub repositories
git:
repo: [email protected]:greys/www.desktop-virtualization.com.git
version: master
dest: /storage/www/dekstop-virtualization.com
accept_hostkey: yes
force: yes

Here are the parameters and how they work in this example:

  • repo is the URL of the repository. In my case it’s a private repo I access using SSH – and the beauty if it is Ansible simply passes my SSH key when pulling this repo down.
  • It’s possible to have a deployment SSH keypair, and to refer to SSH key within Ansible code – I just don’t know enough about this approach to decide if it’s better than passing my SSH key with SSH agent
  • dest means the directory where you want your git repo downloaded into
  • accept_hostkey helps with avoiding Ansible lock-outs when connecting to new SSH hosts – it will simply accept such key, and you normally would. USE WITH CAUTION if you’re accessing lesser known third parties for git hosting!
  • force confirms that if there are some local changes in destination directory, they will get overwritten – because really you should not be making changes directly on the code or hosting server past this stage, your git repo will be the only place to be updating.

How To Pull Multiple Repos in Same Construct

Previous example is very easy to convert into a multi-repo arrangement.

First, I define a number of hosts as Ansible variables, like this:

VIRTUALHOSTS:

- website: desktop-virtualization.com
  github: [email protected]:greys/www.desktop-virtualization.com.git
- website: ireland.reys.net
  github: [email protected]:greys/www.ireland.reys.net.git

Then, I change the git deployment code:

name: Deploy website code from GitHub repositories
git:
repo: "{{ item.github }}"
version: master
dest: /storage/docker/www/{{ item.website }}
accept_hostkey: yes
force: yes
with_items:
- "{{ VIRTUALHOSTS }}"

Really simple yet powerful enough for most needs.

Thanks for stopping by and chat later!

See Also




Contact Me

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!
Recent Articles
03 Sep 2024

macOS Sequoia 15.0

02 Sep 2024

Video: Has This Worked Before?

24 Mar 2024

Homelab: Mac Pro 2013 32GB

Recent Tweets