If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
Right now, both my EC2 machine and my virtual machine are configured to respond to
mootube.l
. And that means I can only access one at a time: I can setup my /etc/hosts
to make mootube.l
point to my EC2 instance or my VM... but not both.
What if instead, we setup the VM to be mootube.l
and the EC2 instance to be
mootube.ec2
? That would fix it! How can we do that?
The problem is that in our roles/nginx/vars/main.yml
file, we have a server_name
variable... but it's just hardcoded to mootube.l
:
server_name: mootube.l |
I want to override that variable to a different value for each host group. And that is totally possible.
But first, re-open /etc/hosts
and point mootube.l
back to the virtual machine
IP. Then, add a new mootube.ec2
entry that points to the EC2 instance:
sudo vim /etc/hosts
# /etc/hosts
# ...
192.168.33.10 mootube.l
54.205.128.194 mootube.ec2
Nice!
Now, how can we override the server_name
variable only for the aws
host group?
Create a new directory called group_vars
with a file inside: aws.yml
. Just
by having this exact directory and filename, whenever the aws
group is executed,
it will automatically load this file and use the variables inside. But those variables
will only apply to the aws
group.
Inside, create a new host_server_name
variable set to mootube.ec2
:
host_server_name: ec2-54-205-128-194.compute-1.amazonaws.com |
Copy that variable name. Next, open roles/nginx/vars/main.yml
, replace the hardcoded
mootube.l
with something fancier: {{ host_server_name|default('mootube.l') }}
:
server_name: "{{ host_server_name|default('mootube.l') }}" |
This says: use host_server_name
if it exists. But if it doesn't, default to mootube.l
.
This should give us a unique host_name
variable for each group.
We're ready: try the playbook:
ansible-playbook ansible/playbook.yml -i ansible/hosts.ini --ask-vault-pass
Nice - we can see a few changes, but only to the EC2 server, as it changes the host name. Ding!
Go back to your browser and refresh mootube.l
. This is coming from the VM: I know
because it has data! Now try http://mootube.ec2
. Boom! This comes from EC2. Super
fun.
We just used Ansible to provision an entirely new server on EC2. Could we even use it to launch the server programmatically? Nope! I'm kidding - totally - let's do it!
// 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
}
}