Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Host Group Vars

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $10.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

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!

Setting Variables for a Host Group

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!

Leave a comment!

11
Login or Register to join the conversation
Ronaldo G. Avatar
Ronaldo G. Avatar Ronaldo G. | posted 4 years ago

Hi,
I have been following this tutorial with great success until now.
I keep getting 500 error from the server running on aws but the logs show nothing.
Anything I missed here?

Reply

Hey Ronaldo,

Do you mean Symfony logs show nothing? If so, then the problem might be with write permissions to var/ directory, you can try to make them 777 for debugging and try again, then check logs to see something there. Or btw, it might be an issue on server level, that means you see the 500 error from your Nginx/Apache web server, and so try to check logs of Nginx/Apache instead. If the problem on web server level - you will see nothing in Symfony logs.

I hope this helps!

Cheers!

Reply
Ronaldo G. Avatar
Ronaldo G. Avatar Ronaldo G. | Victor | posted 4 years ago

I am not a symfony user, only using it during this tutorial, don't know where to find its logs.
The logs I have been looking into are the nginx ones, which are empty.
A request to mootube.ec2 return 500 and there is nothing about the error in any of the logs I can find.

I just tried changing the permission on the var folder and subfolder but no luck

Reply

Hey Ronaldo,

If you look at Nginx logs and find nothing there - then most probably the problem in the application. To find Symfony logs - go to "var/log/" or "var/logs/" depends on your Symfony version, and look for prod.log there. Or, basically, when you're in a root folder you can tail logs with the next command:
$ tail -f var/log/prod.log

Cheers!

Reply
Ronaldo G. Avatar
Ronaldo G. Avatar Ronaldo G. | Victor | posted 4 years ago

I have no prod.log file at all.
I have already looked around for other log files that could give me a clue as for what is wrong but nothing seems to be logged

Reply
Pascal Avatar

Hey Ronaldo,

Hm, it should be something either in Nginx logs or in Symfony application logs. At least, in Nginx you should be able to see that Nginx was handled your request. IIRC the log file is called access.log in Nginx. If you even don't see any logs there - probably your web server is configured wrong, but it would mean you won't see 500 error. So, if you see 500 error - most probably Nginx is able to response to your requests. I'd recommend you to try to do a crazy thing and set 777 permissions for the entire project for debugging purposes. But first, try to clear and warmup the cache, i.e. inside your project:
$ bin/console cache:clear
$ bin/console cache:warmup
$ chmod -R 777 .

And then try to access the website again in a browser.

Cheers!

Reply
Ronaldo G. Avatar
Ronaldo G. Avatar Ronaldo G. | Pascal | posted 4 years ago

Nginx is responding and it is logging the access in access.log, that is the only log I have

Reply
Ronaldo G. Avatar
Ronaldo G. Avatar Ronaldo G. | Pascal | posted 4 years ago | edited

I get this when I try to clear the cache

PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "SensioGeneratorBundle" from namespace "Sensio\Bundle\GeneratorBundle".<br />Did you forget a "use" statement for another namespace? in /var/www/project/app/AppKernel.php:28<br />Stack trace:<br />#0 /var/www/project/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(396): AppKernel->registerBundles()<br />#1 /var/www/project/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(114): Symfony\Component\HttpKernel\Kernel->initializeBundles()<br />#2 /var/www/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(68): Symfony\Component\HttpKernel\Kernel->boot()<br />#3 /var/www/project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(117): Symfony\Bundle\FrameworkBundle\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))<br />#4 /var/www/project/bin/console(29): Symfony\Component\Console\Appli in /var/www/project/app/AppKernel.php on line 28

Reply

Hey Ronaldo,

Ah, yes, you need to clear the cache in prod environment because SensioGeneratorBundle is installed in dev env only. So, try to clear the cache with:
$ bin/console cache:clear --env=prod
$ bin/console cache:warmup --env=prod

It should be executed without errors. Then change permissions to 777.

Cheers!

Reply
Ronaldo G. Avatar
Ronaldo G. Avatar Ronaldo G. | Victor | posted 4 years ago

Ok, I figured what was wrong.
It turns out I was looking for symfony logs in the wrong log folder, there is a var/logs in the project folder that I missed completely.
The prod.log gave me a clue as for where to look and the problem was that for some reason my database was created but it was completely empty, no tables.

One other problem I ran into, which I fixed manually, was that the ansible setup has problems login into mysql with no password which I manage to bypass just by adding skip-grant-tables in the mysql conf but this should definitely not be there.

All working now.

Reply

Hey Ronaldo,

Yeah, depends on Symfony versions logs might be in var/log/ or var/logs/ directory, glad your solved your problems. And thanks for your feedback about what exactly was wrong, it may help other users.

Cheers!

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