Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Hosts & the Inventory File

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.

When we run ansible, we see a few warnings on top:

Host file not found

with a path to a host file somewhere on your system. Then it says:

provided hosts list is empty, only localhost is available.

It turns out, at first, we can only execute ansible against one host: localhost. If you want to start running against any other server, you need to create a host configuration file. You can either do this in a global hosts file - in the location described in the warning - or you can create a file right inside your project. That's the way I like to do it!

In your project, create a new directory called ansible. And inside, make a new hosts.ini file.

The smallest thing you need to configure a host is... just the IP address: 127.0.0.1:

127.0.0.1

We'll keep running things against our local machine for a bit longer.

As soon as you have this, you can use this as your host: ansible 127.0.0.1 -m ping. To tell Ansible about the new hosts file, add -i ansible/hosts.ini. It's -i because the hosts file is known as your inventory. Try it!

ansible 127.0.0.1 -m ping -i ansible/hosts.ini

Setting Host Variables

Ah! It fails! I keep telling you that Ansible works by connecting over SSH and then running commands. Well, technically, that's not 100% true: you can actually configure Ansible to connect to your server in different ways, though you'll almost always use SSH. The most common exception is when you're working on your local machine - you don't need to connect via SSH at all!

To tell Ansible that this is a local connection, in your hosts.ini file, after the IP address, add ansible_connection=local:

127.0.0.1 ansible_connection=local
... lines 2 - 3

There's also a docker connection type if you're getting nerdy with Docker.

Try that ping again!

ansible 127.0.0.1 -m ping -i ansible/hosts.ini

Got it!

This little change is actually really important. By saying ansible_connection=local, we are setting a variable inside of Ansible. And as we build out more complex Ansible configuration, this idea of setting and using variables will become more important. As you'll see, you can set more variables for each host, which will let us change behavior on a host-by-host basis.

In this case, ansible_connection is a built-in variable that Ansible uses when it connects. We're simply changing it first.

Host Groups

So right now, we have just one host. But eventually, you might have many - like 5 web server hosts, 2 database hosts and a Redis host. One common practice is to group your hosts. Let me show you: at the top, add a group called [local], with our one host below it:

[local]
127.0.0.1 ansible_connection=local
... lines 3 - 4

As soon as we do that, instead of using the IP address in the command, we can use the group name:

ansible local -m ping -i ansible/hosts.ini

That will run the module against all hosts inside of the local group... which is just one right now. Boring! Let's add another! Below the first, add localhost with ansible_connection=local:

[local]
127.0.0.1 ansible_connection=local
localhost ansible_connection=local
... lines 4 - 5

This is a little silly, but it shows how this works. Run the command now!

ansible local -m ping -i ansible/hosts.ini

Yes! It runs the ping module twice: once on each server. If you needed to setup 10 web servers... well, you can imagine how awesome this could be.

And actually, there's a special option - --list-hosts that can show you all of the hosts in that group:

ansible local --list-hosts -i ansible/hosts.ini

Ok, remove the localhost line:

[local]
127.0.0.1 ansible_connection=local
... lines 3 - 4

Time to start executing things against a real server.

Leave a comment!

1
Login or Register to join the conversation
Default user avatar
Default user avatar Tunnelix Power | posted 4 years ago

Hey, nice article.. You can have a look what at this article too https://tunnelix.com/an-age...

Reply
Cat in space

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

This tutorial is built using an older version of Symfony, but the core concepts of Ansible are still valid. New versions of Ansible may contain some features that we don't use here.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.12
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.0
        "snc/redis-bundle": "^2.0", // 2.0.0
        "predis/predis": "^1.1", // v1.1.1
        "composer/package-versions-deprecated": "^1.11" // 1.11.99
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.8
        "symfony/phpunit-bridge": "^3.0", // v3.1.4
        "doctrine/data-fixtures": "^1.1", // 1.3.3
        "hautelook/alice-bundle": "^1.3" // v1.4.1
    }
}
userVoice