Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set password to sqlcmd with env variable for security #153

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions tasks/input_sql_files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

- name: Prepare MSSQL and facts for logging in
include_tasks: verify_password.yml
vars:
__mssql_password: "{{ mssql_password }}"
when: >-
(__mssql_sqlcmd_login_cmd is none) or
(__mssql_sqlcmd_login_cmd is not defined)
Expand Down
4 changes: 2 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@
block:
- name: Prepare MSSQL and facts for logging in
include_tasks: verify_password.yml
vars:
__mssql_password: "{{ mssql_password }}"

- name: Check if the set password matches the existing password
command: "{{ __mssql_sqlcmd_login_cmd }} -Q 'SELECT @@VERSION'"
environment:
- SQLCMDPASSWORD: "{{ mssql_password | quote }}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it need to be quoted? not sure how environment variables work in ansible, but if you do something like in python os.environ["SQLCMDPASSWORD"] = somevalue then somevalue does not have to be shell quoted

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, this should not be shell quoted. I fixed that.

ignore_errors: true
changed_when: false
register: __mssql_password_query
Expand Down
3 changes: 2 additions & 1 deletion tasks/sqlcmd_input_file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
- name: Input {{ item }} with the sqlcmd command
command: >-
{{ __mssql_sqlcmd_login_cmd }} -i {{ __mssql_sql_tempfile.path }} -b
environment:
- SQLCMDPASSWORD: "{{ mssql_password | quote }}"
register: __mssql_sqlcmd_input
changed_when: '"successfully" in __mssql_sqlcmd_input.stdout'
no_log: true
always:
# Role prints the output if the input succeeds, otherwise Ansible prints the
# output from the failed input tasks
Expand Down
5 changes: 2 additions & 3 deletions tasks/verify_password.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
changed_when: false
register: __mssql_custom_ip_address

- name: Set a fact with a password query
- name: Set a fact with a login command
vars:
__ipaddress: >-
{{ __mssql_custom_ip_address.stdout |
Expand All @@ -34,5 +34,4 @@
{{ __s_arg }}
{{ __ipaddress_arg }}{{ ',' if __tcpport
else '' }}{{ __tcpport if __tcpport else '' }}
-U sa -P {{ __mssql_password | quote }}
no_log: true
-U sa
4 changes: 2 additions & 2 deletions tests/tasks/verify_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
name: linux-system-roles.mssql
tasks_from: verify_password
public: true
vars:
__mssql_password: "{{ __verify_mssql_password }}"

- name: Wait for mssql-server to start and prepare for client connections
wait_for:
Expand All @@ -71,6 +69,8 @@

- name: Check if the set password matches the existing password
command: "{{ __mssql_sqlcmd_login_cmd }} -Q 'SELECT @@VERSION'"
environment:
- SQLCMDPASSWORD: "{{ __verify_mssql_password | quote }}"
ignore_errors: true
changed_when: false
register: __mssql_password_query
Expand Down