How can I complete a task to complete a task only when creating my database?

I have a migration step that I need to fake after loading the database and looks something like this:

- name: "Setup database" mysql_db: name=my_database state=present target=/tmp/database.sql args: login_user: root login_password: "" - name: Import the database shell: ../../vendor/bin/propel up --fake args: chdir: /var/www/project/application/propel 

Obviously, it would be nice to fake database migration after the first time. How can I do this only once?

+5
source share
4 answers

So, if I understood correctly, you want to import the database, which will be launched only when the Setup database has added the database. Register the variable in the Setup database and use it with the when clause in the Import database:

 - name: "Setup database" mysql_db: name=my_database state=present target=/tmp/database.sql args: login_user: root login_password: "" register: db_created - name: Import the database shell: ../../vendor/bin/propel up --fake args: chdir: /var/www/project/application/propel when: db_created.changed 
+12
source

I liked both of these answers, and I used them to do the following, which was a much cleaner solution for me. It is a pity that the task repeats itself, but it works as you expected, the import occurs only if it is necessary to create a database.

 - name: Create DB (if necessary) mysql_db: name=my_database state=present register: db_created - name: Import DB (if it was created) mysql_db: name=my_database state=import target=/tmp/database.sql when: db_created.changed 
+3
source

Pasi already gave you the answer to your question, but even if you want to skip the database creation step, if it already exists, you can even add check it, for example:

 - name: check if DB exists shell: mysql -e 'SHOW DATABASES;' | grep my_database register: dbstatus ignore_errors: True - name: "Setup database" mysql_db: name=my_database state=present target=/tmp/database.sql args: login_user: root login_password: "" register: db_created when: dbstatus.rc != 0 - name: Import the database shell: ../../vendor/bin/propel up --fake args: chdir: /var/www/project/application/propel when: db_created.changed 

Hope this helps you. Thanks

+1
source

I would only run this game once, if you know that you are setting it up for the first time, but if you do not have it, you can use the variable set at runtime only to run when you know it the first time

 - name: Import the database shell: ../../vendor/bin/propel up --fake args: chdir: /var/www/project/application/propel when: "first_run | default(false)" 

Run it using ansible-playbook main.yml -e 'first_run=1'

If you know that this is not the first setting, just run it without this variable: ansible-playbook main.yml

0
source

Source: https://habr.com/ru/post/1211192/


All Articles