Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Symfony Console Commands

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

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
... lines 3 - 9
tasks:
... lines 11 - 119
- name: Checkout Git repository
git:
repo: https://github.com/knpuniversity/ansible.git
... 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
... lines 3 - 10
tasks:
... lines 12 - 151
# Symfony console commands
- name: Create DB if not exists
... 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:
... line 5
symfony_root_dir: /var/www/project
... lines 7 - 8
symfony_console_path: "{{ symfony_root_dir }}/bin/console"
... lines 10 - 174

Use that in the command: {{ symfony_console_path }} doctrine:database:create --if-not-exists:

---
- hosts: vb
... lines 3 - 10
tasks:
... lines 12 - 151
# Symfony console commands
- name: Create DB if not exists
command: '{{ symfony_console_path }} doctrine:database:create --if-not-exists'
... 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
... lines 3 - 10
tasks:
... 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'
... 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
... lines 3 - 10
tasks:
... 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'
... 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.

Leave a comment!

54
Login or Register to join the conversation
Default user avatar
Default user avatar dhruv sahu | posted 2 years ago

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": []}

2 Reply

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!

1 Reply
Default user avatar
Default user avatar dhruv sahu | victor | posted 2 years ago

Thanks

Reply

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!

Reply
Default user avatar
Default user avatar dhruv sahu | victor | posted 2 years ago

thanks

Reply
Default user avatar
Default user avatar dhruv sahu | posted 2 years ago

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": []}
____________

1 Reply
Default user avatar

a new error occured after i installed composer dependencies.

Reply

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!

Reply
Default user avatar
Default user avatar dhruv sahu | posted 2 years ago

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 }}"

1 Reply

He ydhruv,

Hm, this sounds like indentation problems, no? Anyway, were you able to fix this yourself? Quotes helped you?

Cheers!

Reply
Default user avatar
Default user avatar dhruv sahu | victor | posted 2 years ago

no nothing worked

1 Reply
Default user avatar
Default user avatar dhruv sahu | posted 2 years ago

please rectify this error

1 Reply

Hey dhruv,

Sorry, but what error exactly are you talking about? Could you give us a bit more context, please?

Cheers!

Reply
Default user avatar
Default user avatar Евгений Явгель | posted 4 years ago

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

1 Reply

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!

Reply
Default user avatar

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.

Reply
ITHouseMeister Avatar
ITHouseMeister Avatar ITHouseMeister | posted 1 year ago

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?

Reply

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!

Reply
Default user avatar

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.

Reply

Hey Uiuytio,

Thanks for sharing your workaround with others!

Cheers!

Reply
Default user avatar

Hi guys,
I am having some problems when trying to create the DB from the playbook.

TASK [Create DB if not exists] ************************************************************************************************************************************************************************************************
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}

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!

Reply

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!

Reply

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'


Reply

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!

Reply

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:


TASK [Create DB if not exists] ************************************************************************************************************************************************************************************************
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}

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.

Reply

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!

Reply

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!

Reply

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!

Reply
Default user avatar

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 vagrant 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.

Reply

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!

1 Reply
Default user avatar
Default user avatar toporovvv | posted 4 years ago

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 Евгений Явгель 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/

Reply

Hey toporovvv ,

Thanks for sharing your solution with others!

Cheers!

Reply
Jeffrey C. Avatar
Jeffrey C. Avatar Jeffrey C. | victor | posted 3 years ago

Wait do you need the directory's all the way to var or just the file inside project?
I'm quite confused.

Reply

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!

Reply
Jeffrey C. Avatar
Jeffrey C. Avatar Jeffrey C. | victor | posted 3 years ago

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!

Reply

Hey Emin,

Could you clarify what are you trying to do, please? It depends on what you need.

Cheers!

Reply
Jeffrey C. Avatar
Jeffrey C. Avatar Jeffrey C. | victor | posted 3 years ago

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.

Reply

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!

Reply
Jeffrey C. Avatar
Jeffrey C. Avatar Jeffrey C. | victor | posted 3 years ago

Hey Victor,

Aah i see now i get it thank man! :)

Cheers!

Reply

You're welcome! Glad you've figured it out.

Cheers!

Reply
Default user avatar
Default user avatar mlavrik | posted 4 years ago

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!

Reply

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!

Reply
Default user avatar

Thanks! Adding version parameter helps.

Reply
Default user avatar
Default user avatar Dayron Gallardo | posted 4 years ago

I'm getting this error -> There are no commands defined in the "doctrine:migrations" :(

Reply
Default user avatar

The only thing that helped me was restart the host.

Reply

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!

Reply
Default user avatar
Default user avatar jian su | posted 4 years ago

Hi guys: for some reason the CSS is off... Is it because we did not configure boostrap theme in ansible?

Reply

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!

Reply
Default user avatar
Default user avatar sokphea chea | posted 4 years ago

Hello, I still get the cache error on TASK [Install Composer's dependencies] after I remove the cache, is there something I need to check? thanks

Reply

Hey Chea,

May I see your full error output here? You can also wrap this output with pre/code tags in your comment to avoid getting to the spam folder - see Syntax Highlighting section in https://help.disqus.com/cus...

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
    }
}