Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Ansistrano Role Installation

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

We already have an ansible/ directory, which has a bunch of files to support two playbooks: aws.yml that boots new EC2 servers and playbook.yml that can provision those servers, installing things like Nginx, PHP, and anything else we need. Now, we're going to create a third playbook: deploy.yml that will deploy our code.

But! There's one really important thing I want you to understand: this new playbook will not use any of the files inside of the ansible/ directory. So, don't worry or think about them: pretend that the ansible/ directory is completely empty, except for deploy.yml. If you do need any other files, we will talk about them!

To help us deploy with Ansible, we're going to - of course - use Ansistrano! Open up ansistrano.com in your browser. It has some cool deployment stats... but the most important thing is the ansistrano.deploy link that goes to the GitHub page and their docs.

Ansistrano is an Ansible role... which basically means it gives us free Ansible tasks! That's like getting a free puppy... but without all that responsibility and carpet peeing!

Installing the Role

The docs show an ansible-galaxy command that will install the role. Don't do it! There's a better way!

Open ansible/requirements.yml:

- src: DavidWittman.redis
version: 1.2.4

You can use ansible-galaxy to install whatever random Ansible role you want. Or, you can describe the roles you need in a YAML file and tell galaxy to install everything you need at once. This is just a nicer way to keep track of what roles we need.

Add another src: line. Then, go copy the role name - just the deploy role:

... lines 1 - 3
- src: ansistrano.deploy
... lines 5 - 6

Tip

Due to the changes in Ansible Galaxy, Ansistrano is installed now via ansistrano.deploy instead of the old carlosbuenosvinos.ansistrano-deploy.

We'll talk about rollback later. Paste that and add version. So... what's the latest version of this role? Let's find out! On the GitHub page, scroll up and click "Releases". But be careful! There are actually newer tags. Ok, so right now, the latest version is 2.7.0. Add that to requirements.yml:

... lines 1 - 3
- src: ansistrano.deploy
version: 2.7.0

Great! To make sure all of the roles are installed, run:

ansible-galaxy install -r ansible/requirements.yml

We already have the Redis role installed that's used in the provision playbook. And now it downloads ansistrano-deploy to some /usr/local/etc directory. Perfect!

Configuring the Hosts

In our deploy.yml, start with the meaningless, but ceremonial three dashes. Then, below that, add hosts set to aws:

---
- hosts: aws
... lines 3 - 6

This is important: if you're only using Ansible for deployment, then you don't need any of these other files in the ansible/ directory... except for hosts.ini. You do need this file. It doesn't need to be as complex as mine. You just need to have one host group with at least one IP address below it:

... lines 1 - 6
[aws]
54.205.128.194
... lines 9 - 13

In our case, we have a host group called aws with the IP address to one server below it.

Using the Role

Back in deploy.yml, let's import the role! Add roles:, copy the name of the role, and then paste it here: ansistrano.deploy:

---
- hosts: aws
roles:
- ansistrano.deploy

If you went through our Ansible tutorial, then you know that a role magically gives our playbook new tasks! Plus, a few other things, like variables and handlers.

So... what new tasks did this add? Let's find out! Run:

ansible-playbook -i ansible/hosts.ini ansible/deploy.yml --list-tasks

Thanks to the --list-tasks flag, this won't execute our playbook, it will just tell us what tasks it would run. Try it!

Not all of this will make sense yet... but you can see things like "Ensure deployment base path exists". And later, it creates something called a "current folder" and performs some cleanup.

What does this all mean? It's time to learn exactly how Ansistrano works and run our first deploy. That's next!

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

While the fundamentals of Ansistrano haven't changed, this tutorial is built using Symfony 3, which has significant differences versus Symfony 4 and later.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "doctrine/doctrine-bundle": "^1.6", // 1.6.8
        "doctrine/orm": "^2.5", // v2.7.2
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "sensio/distribution-bundle": "^5.0.19", // v5.0.20
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.26
        "symfony/monolog-bundle": "^3.1.0", // v3.1.0
        "symfony/polyfill-apcu": "^1.0", // v1.4.0
        "symfony/swiftmailer-bundle": "^2.3.10", // v2.6.3
        "symfony/symfony": "3.3.*", // v3.3.5
        "twig/twig": "^1.0||^2.0", // v1.34.4
        "doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.1
        "predis/predis": "^1.1", // v1.1.1
        "composer/package-versions-deprecated": "^1.11" // 1.11.99
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.6
        "symfony/phpunit-bridge": "^3.0", // v3.3.5
        "doctrine/data-fixtures": "^1.1", // 1.3.3
        "hautelook/alice-bundle": "^1.3" // v1.4.1
    }
}

What Ansible libraries does this tutorial use?

# ansible/requirements.yml
-
    src: DavidWittman.redis
    version: 1.2.4
-
    src: ansistrano.deploy
    version: 2.7.0
-
    src: ansistrano.rollback
    version: 2.0.1
userVoice