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

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!

  • 2019-07-01 Victor Bocharsky

    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!

  • 2019-06-27 Ronaldo Junior

    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.

  • 2019-06-21 Victor Bocharsky

    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!

  • 2019-06-21 Ronaldo Junior

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

  • 2019-06-21 Ronaldo Junior

    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".
    Did you forget a "use" statement for another namespace? in /var/www/project/app/AppKernel.php:28
    Stack trace:
    #0 /var/www/project/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(396): AppKernel->registerBundles()
    #1 /var/www/project/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(114): Symfony\Component\HttpKernel\Kernel->initializeBundles()
    #2 /var/www/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(68): Symfony\Component\HttpKernel\Kernel->boot()
    #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))
    #4 /var/www/project/bin/console(29): Symfony\Component\Console\Appli in /var/www/project/app/AppKernel.php on line 28

  • 2019-06-20 Victor Bocharsky

    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!

  • 2019-06-20 Ronaldo Junior

    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

  • 2019-06-19 Victor Bocharsky

    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!

  • 2019-06-19 Ronaldo Junior

    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

  • 2019-06-18 Victor Bocharsky

    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!

  • 2019-06-18 Ronaldo Junior

    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?