From aa671184f2be21f2ba267d18d4a4b3e9f0ea3f49 Mon Sep 17 00:00:00 2001 From: "Zeno F. Pensky" Date: Mon, 1 Jul 2019 16:58:07 +0200 Subject: [PATCH 1/2] AdHoc (sql) Add post import task This allows the user tu run post import tasks with this role --- defaults/main.yml | 5 +++++ tasks/main.yml | 6 ++++++ tasks/post_import.yml | 5 +++++ 3 files changed, 16 insertions(+) create mode 100644 tasks/post_import.yml diff --git a/defaults/main.yml b/defaults/main.yml index da3d4ac..dc0f5ab 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -26,3 +26,8 @@ ## http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html import_db_aws_access_key_id: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}" import_db_aws_secret_access_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}" + +## Define some sql queries, that are executed after the db is imported +import_db_post_import_sql_queries: [] + ## The query that should be executed + # - query: "" diff --git a/tasks/main.yml b/tasks/main.yml index db53c6f..1a97228 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,3 +1,9 @@ --- - include: "dependencies.yml" - include: "import.yml" + +- name: "Run post import quries" + include: "post_import.yml" + with_items: "{{ import_db_post_import_sql_queries }}" + loop_control: + loop_var: "import_db_post_import_sql_query" diff --git a/tasks/post_import.yml b/tasks/post_import.yml new file mode 100644 index 0000000..f8417b9 --- /dev/null +++ b/tasks/post_import.yml @@ -0,0 +1,5 @@ +--- +- name: "Execute sql query" + command: > + mysql --user=root --password={{ import_db_root_password }}" {{ import_db_database_name }}" + --execute="{{ import_db_post_import_sql_query.query }}" From 107fc028b093cfb160ac20395892f5109a2b2a1e Mon Sep 17 00:00:00 2001 From: "Zeno F. Pensky" Date: Mon, 1 Jul 2019 21:45:12 +0200 Subject: [PATCH 2/2] Adhoc (post import sql) Do not print pw As ansible is printing the passwords into the log, we need to prevent this to happen. Therefor we disable logging, save the output to a variable and remove the password from the output before printing it. This also fixes a syntax error. --- tasks/post_import.yml | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tasks/post_import.yml b/tasks/post_import.yml index f8417b9..f11dea5 100644 --- a/tasks/post_import.yml +++ b/tasks/post_import.yml @@ -1,5 +1,21 @@ --- - name: "Execute sql query" command: > - mysql --user=root --password={{ import_db_root_password }}" {{ import_db_database_name }}" + env MYSQL_PWD={{ import_db_root_password }} + mysql --user=root {{ import_db_database_name }} --execute="{{ import_db_post_import_sql_query.query }}" + no_log: true + ignore_errors: true + register: import_db_post_import_sql_query_result + +- debug: + msg: '{{ import_db_post_import_sql_query_result.stderr | regex_replace(import_db_root_password) }}' + when: import_db_post_import_sql_query_result.stderr != "" + +- debug: + msg: '{{ import_db_post_import_sql_query_result.stdout | regex_replace(import_db_root_password) }}' + when: import_db_post_import_sql_query_result.stdout != "" + +- fail: + msg: "error in sql querry" + when: import_db_post_import_sql_query_result.stderr != ""