Chapters
-
Course Code
Subscribe to download the code!Compatible PHP versions: >=5.5.9
Subscribe to download the code!Compatible PHP versions: >=5.5.9
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
Symfony Console Commands
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
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.
The project is working! Except... that it's not actually our project: this is the Symfony Standard Edition. Our cow customers are waiting: let's install MooTube!
Head over to https://github.com/knpuniversity/ansible to find the code behind this project. Copy the clone URL and open your editor. Find the spot where we clone the repo and use our new URL:
--- | |
- hosts: vb | |
Show Lines
|
// ... lines 3 - 9 |
tasks: | |
Show Lines
|
// ... lines 11 - 119 |
- name: Checkout Git repository | |
git: | |
repo: https://github.com/knpuniversity/ansible.git | |
Show Lines
|
// ... lines 123 - 163 |
You know the drill: run the playbook!
ansible-playbook ansible/playbook.yml -i ansible/hosts.ini
Our repository is public... which makes life easy. If you have a private repository,
you'll need to make sure that your server has access to it. They talk about that
a bit in the git
module docs. You can also use a deploy key.
Using the Console
Once we have the code, we need to setup a few other things, like the database.
The README.md
file talks about these: after you download the composer dependencies,
you can set up the database by running these three commands. Each runs through Symfony's
console: an executable file in the bin/
directory.
This is a perfect situation for the command
module... because... well, we literally
just need to run 3 commands. Head to your playbook. Right above the handlers, add
a comment: "Symfony Console Commands". We'll start with a task called "Create DB
if not exists":
--- | |
- hosts: vb | |
Show Lines
|
// ... lines 3 - 10 |
tasks: | |
Show Lines
|
// ... lines 12 - 151 |
# Symfony console commands | |
- name: Create DB if not exists | |
Show Lines
|
// ... lines 154 - 174 |
Use the command
module. For the value... we need to know the path to that bin/console
file.
This is another good spot for a variable! Create a new one called symfony_console_path
set to {{ symfony_root_dir }}/bin/console
:
--- | |
- hosts: vb | |
vars: | |
Show Lines
|
// ... line 5 |
symfony_root_dir: /var/www/project | |
Show Lines
|
// ... lines 7 - 8 |
symfony_console_path: "{{ symfony_root_dir }}/bin/console" | |
Show Lines
|
// ... lines 10 - 174 |
Use that in the command: {{ symfony_console_path }} doctrine:database:create --if-not-exists
:
--- | |
- hosts: vb | |
Show Lines
|
// ... lines 3 - 10 |
tasks: | |
Show Lines
|
// ... lines 12 - 151 |
# Symfony console commands | |
- name: Create DB if not exists | |
command: '{{ symfony_console_path }} doctrine:database:create --if-not-exists' | |
Show Lines
|
// ... lines 155 - 174 |
That last flag prevents an error if the database is already there.
Awesome! Copy that task to create the second one: "Execute migrations". Use
doctrine:migrations:migrate --no-interaction
:
--- | |
- hosts: vb | |
Show Lines
|
// ... lines 3 - 10 |
tasks: | |
Show Lines
|
// ... lines 12 - 151 |
# Symfony console commands | |
- name: Create DB if not exists | |
command: '{{ symfony_console_path }} doctrine:database:create --if-not-exists' | |
- name: Execute migrations | |
command: '{{ symfony_console_path }} doctrine:migrations:migrate --no-interaction' | |
Show Lines
|
// ... lines 158 - 174 |
And add one more: "Load data fixtures". This is something that we only want to run if this is a development machine, because it resets the database. We'll talk about controlling that later.
For this command, use hautelook_alice:doctrine:fixtures:load --no-interaction
:
--- | |
- hosts: vb | |
Show Lines
|
// ... lines 3 - 10 |
tasks: | |
Show Lines
|
// ... lines 12 - 151 |
# Symfony console commands | |
- name: Create DB if not exists | |
command: '{{ symfony_console_path }} doctrine:database:create --if-not-exists' | |
- name: Execute migrations | |
command: '{{ symfony_console_path }} doctrine:migrations:migrate --no-interaction' | |
- name: Load data fixtures | |
command: '{{ symfony_console_path }} hautelook_alice:doctrine:fixtures:load --no-interaction' | |
Show Lines
|
// ... lines 161 - 174 |
Ok! The 3 commands are ready! Head back to the terminal. Woh! It exploded!
And actually... the reason is not that important: it says an error occurred during
the cache:clear --no-warmup
command. After we run composer install
, Symfony
runs several post install commands. One clears the cache. Changing from one project
to an entirely different project temporarily put things in a weird state. This one
time, in the virtual machine, just remove the cache manually:
rm -rf var/cache/*
Try the playbook now:
ansible-playbook ansible/playbook.yml -i ansible/hosts.ini
This time composer install
should work and hopefully our new commands will setup
the database. By the way! A Symfony 3 app reads its configuration from a parameters.yml
file... which is not committed to the repository. So... in theory, that file should
not yet exist... and none of this should work. But that file does exist! Why?
Thanks to a special line in composer.json
, after composer install
finishes, the
parameters.yml.dist
file is copied to parameters.yml
. And thanks to that dist
file, Symfony will try to connect to MySQL using the root
user and no password.
If that's not right, just modify the file on the VM directly for now. Later, we'll
talk about how we could properly update this file.
Yes! It worked! Notice: the three new tasks all say changed. That's because the
command
module isn't smart enough to know whether or not these actually changed
anything. But, more on that soon!
Find your browser and refresh! Welcome to MooTube! The fact that it's showing these videos means our database is working. Now, let's talk about tags: a cool way to help us run only part of our playbook.
56 Comments
Hey dhruv,
From the output, I see the problem is that you don't have Composer dependencies installed. Please, make sure you run "composer install" first before calling any Symfony console commands.
I hope this helps!
Cheers!
Thanks
Hey dhruv,
It looks like I got double "thanks" from you on this problem :D did you hit this problem again? ;) Anyway, glad it was useful one more time for you!
Cheers!
thanks
For those who faced an issue with hautelook/alice-bundle
:
remote: Repository not found.
fatal: repository 'https://github.com/hautelook/AliceBundle.git/' not found`
You can use my fork https://github.com/ksxauh/ansible.git
with a fix as a Git repository in the "Checkout Git repository" ansible task
P.S. I've created a PR to the original repo as well: https://github.com/knpuniversity/ansible/pull/11 //cc
Missing repo Explanation: https://github.com/nelmio/alice/issues/1089
Hey Kirill,
Thanks for sharing it! Though the hautelook/alice-bundle is now should be available again, see: https://packagist.org/packages/hautelook/alice-bundle
Cheers!
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ["/var/www/project/bin/console", "--if-not-exists", "doctrine:database:create"], "delta": "0:00:00.120540", "end": "2020-02-26 16:37:40.124195", "msg": "non-zero return code", "rc": 1, "start": "2020-02-26 16:37:40.003655", "stderr": "\nIn AbstractMySQLDriver.php line 115:\n \n An exception occured in driver: SQLSTATE[HY000] [1698] Access denied for us \n er 'root'@'localhost' \n \n\nIn PDOConnection.php line 47:\n \n SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' \n \n\nIn PDOConnection.php line 43:\n \n SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' \n \n\ndoctrine:database:create [--shard SHARD] [--connection [CONNECTION]] [--if-not-exists] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>", "stderr_lines": ["", "In AbstractMySQLDriver.php line 115:", " ", " An exception occured in driver: SQLSTATE[HY000] [1698] Access denied for us ", " er 'root'@'localhost' ", " ", "", "In PDOConnection.php line 47:", " ", " SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' ", " ", "", "In PDOConnection.php line 43:", " ", " SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' ", " ", "", "doctrine:database:create [--shard SHARD] [--connection [CONNECTION]] [--if-not-exists] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>"], "stdout": "", "stdout_lines": []}
____________
a new error occured after i installed composer dependencies.
Hey dhruv sahu!
Hmm, it looks like your database password is incorrect for your database:
Access denied for user 'root'@'localhost'
This is happening when you're running bin/console doctrine:database:create
. Have you configured your database credentials in .env
(or parameters.yml, depending on your Symfony version) before you've run this command?
Cheers!
ERROR! Syntax Error while loading YAML.
mapping values are not allowed in this context
The error appears to be in '/etc/ansible/playbook.yml': line 114, column 16, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Create DB if not exists
command: "{{ symfony_console_path }} doctrine:database:create --if-not-exists"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
He ydhruv,
Hm, this sounds like indentation problems, no? Anyway, were you able to fix this yourself? Quotes helped you?
Cheers!
no nothing worked
please rectify this error
Hey dhruv,
Sorry, but what error exactly are you talking about? Could you give us a bit more context, please?
Cheers!
Hi guys. Task 'Execute migrations' give me an error: fatal: [192.168.33.10]: FAILED! => {"changed": true, "cmd": ["/var/www/ansible_project/bin/console", "doctrine:migrations:migrate", "--no-interaction"], "delta": "0:00:00.182210", "end": "2017-05-23 21:41:51.013178", "failed": true, "rc": 1, "start": "2017-05-23 21:41:50.830968", "stderr": "\n \n [Symfony\\Component\\Console\\Exception\\CommandNotFoundException] \n There are no commands defined in the \"doctrine:migrations\" namespace. \n Did you mean one of these? \n doctrine \n doctrine:cache \n doctrine:database \n doctrine:generate \n doctrine:mapping \n doctrine:query \n doctrine:schema \n ", "stderr_lines": ["", " ", " [Symfony\\Component\\Console\\Exception\\CommandNotFoundException] ", " There are no commands defined in the \"doctrine:migrations\" namespace. ", " Did you mean one of these? ", " doctrine ", " doctrine:cache ", " doctrine:database ", " doctrine:generate ", " doctrine:mapping ", " doctrine:query ", " doctrine:schema ", " "], "stdout": "", "stdout_lines": []}
I edited the composer.json: "doctrine/doctrine-migrations-bundle": "1.0.0",
"doctrine/migrations": "1.0.1" AND added in the AppKernel.php this line: new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(). How it resolve? Thanks
Hey Евгений,
Don't you forget to run "composer update" after changing the composer.json file? Because if you change composer.json manually - then you still have outdated composer.lock file, so running "composer install" has no effect.
Maybe there's a difference between versions. Could you try to install the latest version of it? To install and enable the latest version correctly you may follow the installation guide here: https://symfony.com/doc/cur... . And also try to clear the cache and run this command directly on your VM first.
Cheers!
Just blow out your projects dir in the VM and re-run Ansible. Ansible isnt actually pulling the new project files in. You can see that by looking at app/AppKernel.php. The migrations and haute bundles arent even registered because you still have the symfony standard code. Once you blow out the project dir it'll see the change, pull the new code and Composer will install the new packages.
Hi guys,
hmm, no one of your suggestions helps me:
`fatal: [192.168.19.147]: FAILED! => {"changed": true, "cmd": ["/var/www/project/bin/console", "doctrine:migrations:migrate
", "--no-interaction"], "delta": "0:00:00.113318", "end": "2020-10-25 14:55:22.904648", "msg": "non-zero return code", "rc
": 1, "start": "2020-10-25 14:55:22.791330", "stderr": "\n
\n There are no commands defined in the \"doctrine:migrations\" namespace.`
Tested with Ubuntu Xenial, Bionic and Focal Fossa, Sym 3.4.45, Ansible 2.9.x, PHP7.4, PHP7.1...
adding this
` - name: Install Composer requirements for migrations
command: "composer require doctrine/doctrine-migrations-bundle"`
But it won't work. What should I do?
Hey Axel,
From the output I see that you don't have "doctrine:migrations:migrate" command. Please, make sure you have it installed via Composer and enabled in app/AppKernel.php. Also, make sure it's installed and enabled for the environment in which you're trying to execute this command. For debugging, you literally may want to ssh into the container and execute that command manually to see the error.
Cheers!
Encountered the same problem. Took me a lot of digging, but it turns out I was somehow still getting the Symfony default project used earlier in the tutorial (which of course didn't have the right dependencies or had them enabled in AppKernel.php). Nuking everything in /var/www/project (including the .git folder) fixed it.
Hey Uiuytio,
Thanks for sharing your workaround with others!
Cheers!
Hi guys,
I am having some problems when trying to create the DB from the playbook.
TASK [Create DB if not exists] ************************************************************************************************************************************************************************************************<br />fatal: [192.168.33.11]: FAILED! => {"changed": false, "cmd": "/var/www/project/bin/console doctrine:database:create --if-not-exists", "msg": "[Errno 13] Permission denied: b'/var/www/project/bin/console'", "rc": 13}<br />
My .env is fine and when I ssh to the VM I can create the DB manually but with the playbook it seems quite difficult.
Why does it work when I ssh to the VM but not over the playbook? I am supposed to be the same user!
Hey Sayil,
Hm, could you check its permissions? Most probably you don't have executable permissions on it. Also, are you sure the user is the same and you run it without sudo when ssh-ed to the server?
Btw, you can try to add php in front of the command, it might help. Or, just simply try to set permissions that allows execution.
I hope this helps!
Cheers!
I also added a task before the DB is created to make sure that bin/console has permissions, looks like this:
`
- name: Fix permission to create database
become: true
shell: 'chmod 0777 /var/www/project/bin/console'
`
Hey Sayil,
OK, and it still does not work? I'd recommend you to use file module instead: https://docs.ansible.com/an... - see examples below how to change permissions.
So, it's weird as for me, if you set 777 it should just work. Could you double check it has 777 after this task?
Cheers!
Hi Victor,
thanks for answering.
I checked my permissions and changed the bin/console ones. I still have the same problem but a bit different. Now this is the result I am getting:
<br />TASK [Create DB if not exists] ************************************************************************************************************************************************************************************************<br />fatal: [192.168.33.11]: FAILED! => {"changed": false, "cmd": "/var/www/project php bin/console doctrine:database:create --if-not-exists", "msg": "[Errno 13] Permission denied", "rc": 13}<br />
It does not show where the permissions where denied like in the first message. Which permissions should I change?
When I ssh into the VM I can create the DB with the vagrant user and without using sudo.
I changed all the permissions in /var/www/project to try to find the solution but I still get the same message.
What am I missing here? Any Ideas?
I also added the php before the bin/console but still no.
Hey Sayil,
Hm, how do you create DB when you ssh-ed into the VM? Did you use "mysql" or "bin/console doctrine:database:create" command? It makes me think that probably you your DB credentials are incorrect? So, you don't have permissions to create DB, not execute that bin/console. Please, double check that you set DB permissions correctly for your VM machine
Cheers!
Hi Victor,
I create the database with the exact same command from inside the VM, "php bin/console doctrine:database:create". That's why I don't understand what is hapening when I try to create it with the playbook.
After I yhe task to change permissions I still have 777 so I don't understand.
Any ideas?
Thanks in advance!
Hey @Sayil!
Hmm, this is trick! I have 2 questions/comments:
1) If you SSH into the machine, are you able to run the php bin/console doctrine:database:create
? I think the answer is yes (you said so in your previous comment) but I wanted to be absolutely sure :).
2) Are you using the same SSH user for your Ansible script as when you SSH in manually? Again, my guess is "Yes", but I wanted to check ;).
3) Are you able to, for example, do tasks that look like this in ansible?
# this would prove whether or not you can call PHP
php -v
# this would prove that you can "read" the bin/console file
cat bin/console
4) Finally, while I can't explain why you can ssh in and run the command but Ansible cannot, one thing that could be affecting the permissions are network mounts. If your project directory is shared between your host and guest machine, then sometimes setting permissions in your VM is strange... because the file actually exists on your host machine (and is mounted as a network mount inside your VM). It's just something to keep in mind.
Let me know if you find anything! Good luck!
Cheers!
Hi Ryan,
Thank you for your answer.
Yes, I can run the the php bin/console doctrine:database:create
when I ssh into the VM. This was the only way of creating the DB and continue with the tutorial.
Yes, I checked again and it is the same user, in my case <i>vagrant</i> user.
When I ssh into the VM I am able tu run the two commands you suggested without any problems.
I played with the permissions of most files in the VM ans still nothing. I noticed that some of them changed after running the playbook so I changed them in the playbook before creating the DB but nothing.
I will keep looking for the answer and let you know if I find anything.
Thanks for all the support!
Cheers.
Hey @Sayil!
I noticed that some of them changed after running the playbook so I changed them in the playbook before creating the DB but nothing.
I wonder what happens if you ssh into your machine, check the permissions, run chmod -R 777
and then check the permissions immediately after. You may notice that even this seems to not change the permissions. In that case, I would really think that you are hitting a permissions issue because the directory is a "network" directory. It might help you dig ;).
Cheers!
If you face a problem "There are no commands defined in the \"doctrine:migrations\" namespace. \n Did you mean one of these?", whick I and there got after a first playbook execution in this tutorial, you need to delete /var/www/project folder. Because, I suppose, Ansible do not clean it after a changing repo path in a "Checkout Git repository" task. An after that I hadn't any issues with cache - just a proper look of MooTube on url http://mootube.l/
Hey toporovvv ,
Thanks for sharing your solution with others!
Cheers!
Wait do you need the directory's all the way to var or just the file inside project?
I'm quite confused.
Hey Emin,
You need to specify the full path to the directories / files, because each Ansible command is isolated, so if you change the dir in one step, in another step your command still will be executed in the home directory. So, Ansible do not understand the "project" and each step starts from the home directory. But each step may have "args" option where you can change dir with "chdir" sub-option, then, in the module, you can specify a path relative to the changed dir. See examples for "command" module: https://docs.ansible.com/an...
Cheers!
Hey Victor,
Yes thank you for the fast reply now i understand it better but for this to work do i delete it al they to /var/ or only stuff inside /var/www/project?
Cheers!
Hey Emin,
Could you clarify what are you trying to do, please? It depends on what you need.
Cheers!
Hey Victor,
What i mean is do i need to delete the whole /var/www/project map in the way that the path needs to be created again or only in the project file.
Hey Emin,
I suppose you mean this step:
- name: Create project directory and set its permissions
become: true
file:
path: "{{ symfony_root_dir }}"
state: directory
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
recurse: yes
If so - the answer is no, you don't need to delete the /var/www/project/ directory on each run, because in this step Ansible whether create this folder of it does not exist or just skip this step if it's already created. So, it's like Ansible just make sure that this folder always exists and have proper owner/group permissions and that's it.
Cheers!
Hey Victor,
Aah i see now i get it thank man! :)
Cheers!
You're welcome! Glad you've figured it out.
Cheers!
Hello guys. Problem with git checkout task. After changing repo url it fails with message "Could not determine remote revision for HEAD". Debug log - https://pastebin.com/DdF49Hqm
What could be the reason?
Thanks!
Hey mlavrik ,
It's weird enough. Did you change the repo link to another repository? I think pointing the version could fix it. Try:
- name: Checkout Git repository
git:
repo: https://github.com/knpuniversity/ansible.git
dest: "{{ symfony_root_dir }}"
force: yes
version: master
Where "master" is a branch which you want to checkout.
If it doesn't help, please, try to connect to the remove host manually ("vagrant ssh" if you're using VM) and drop the repository folder at all with "rm -rf" command. Then run the playbook - Ansible try to pull the whole repo again without problem I think.
Cheers!
Thanks! Adding version parameter helps.
I'm getting this error -> There are no commands defined in the "doctrine:migrations" :(
The only thing that helped me was restart the host.
Hey Dayron,
Hm, that's a bit weird that you have to restart the host, clearing the cache should be enough. But I'm glad you solved it! And thanks for sharing your solution with other users
Cheers!
Hi guys: for some reason the CSS is off... Is it because we did not configure boostrap theme in ansible?
Hey Jian,
Hm, try to re-install the assets again with "./bin/console assets:install". Does it fix the CSS problem? Actually, this command executes during the "composer install", at the end of it. But if due to the some reason "composer install" command failed it won't be run. Also, try to clear the cache: dev/prod, depends on what environment you're using.
Cheers!
"Houston: no signs of life"
Start the conversation!
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
}
}
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ["/var/www/project/bin/console", "doctrine:database:create", "--if-not-exists"], "delta": "0:00:00.024293", "end": "2020-02-22 12:08:42.155989", "msg": "non-zero return code", "rc": 255, "start": "2020-02-22 12:08:42.131696", "stderr": "PHP Warning: require(/var/www/project/app/../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/project/app/autoload.php on line 9\nPHP Fatal error: require(): Failed opening required '/var/www/project/app/../vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/project/app/autoload.php on line 9", "stderr_lines": ["PHP Warning: require(/var/www/project/app/../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/project/app/autoload.php on line 9", "PHP Fatal error: require(): Failed opening required '/var/www/project/app/../vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/project/app/autoload.php on line 9"], "stdout": "", "stdout_lines": []}