-1

I have my mysql installed on windows server 2012.When i am trying mysql module of ansible using following ansible playbook

- name: Execute mysql dump files
      #win_command: "mysql  -u{{ db_user }} -p  {{ db_database }} < {{ item.path }}"
      mysql_db:
        login_host: "{{ db_host }}"
        login_password: "{{ db_pass }}"
        login_user: "{{ db_user }}"
        login_port: "{{ db_port }}"
        name: "{{ db_database }}"
        state: import
        target: "{{ item.path }}"
      with_items:
          - "{{ files_sql.files }}"
      register: mysql_query

It throwing me following error

failed: [13.82.109.51] (item={u'isdir': False, u'extension': u'.sql', u'isreadonly': False, u'checksum': u'da39a3ee5e6b4b0d3255bfef95601890afd80709', u'size': 0, u'creationtime': 1500861234.9365487, u'filename': u'abcd.sql', u'lastaccesstime': 1500861234.9365487, u'owner': u'BUILTIN\Administrators', u'lastwritetime': 1500861234.9365487, u'islnk': False, u'attributes': u'Archive', u'path': u'C:\Users\atul\Documents\mysql-dev\17-06-2017\abcd.sql', u'isarchive': True, u'ishidden': False, u'isshared': False}) => {"failed": true, "item": {"attributes": "Archive", "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "creationtime": 1500861234.9365487, "extension": ".sql", "filename": "abcd.sql", "isarchive": true, "isdir": false, "ishidden": false, "islnk": false, "isreadonly": false, "isshared": false, "lastaccesstime": 1500861234.9365487, "lastwritetime": 1500861234.9365487, "owner": "BUILTIN\Administrators", "path": "C:\Users\atul\Documents\mysql-dev\17-06-2017\abcd.sql", "size": 0}, "module_stderr": "At line:11 char:23\r\n+ except (AttributeError, OSError):\r\n+ ~\r\nMissing argument in parameter list.\r\n\r\nAt line:13 char:3\r\n+ if scriptdir is not None:\r\n+ ~\r\nMissing '(' after 'if' in if statement.\r\n\r\nAt line:20 char:3\r\n+ if sys.version_info < (3,):\r\n+ ~\r\nMissing '(' after 'if' in if statement.\r\n\r\nAt line:20 char:26\r\n+ if sys.version_info < (3,):\r\n+ ~\r\nMissing expression after ','.\r\n\r\nAt line:20 char:21\r\n+ if sys.version_info < (3,):\r\n+ ~\r\nThe '<' operator is reserved for future use.\r\n\r\nAt line:27 char:5\r\n+
from io import BytesIO as IOStream\r\n+ ~~~~\r\nThe 'from' keyword is not supported in this version of the language.\r\n\r\nAt line:29 char:5\r\n+ from StringIO import StringIO as IOStream\r\n+
~~~~\r\nThe 'from' keyword is not supported in this version of the language.\r\n\r\nAt line:31 char:25\r\n+ def invoke_module(module, modlib_path, json_params):\r\n+ ~\r\nMissing argument in parameter list.\r\n\r\nAt line:33 char:7\r\n+ if pythonpath:\r\n+ ~\r\nMissing '(' after 'if' in if statement.\r\n\r\nAt line:34 char:57\r\n+
os.environ['PYTHONPATH'] = ':'.join((modlib_path, pythonpath))\r\n+
~\r\nMissing argument in parameter list.\r\n\r\nNot all parse errors were reported. Correct the reported errors and try again.\r\n\r\n\r\n + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx \r\n ception\r\n + FullyQualifiedErrorId : MissingArgument\r\n \r\n\r\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}

I have also try by running mysql on command prompt but that is also throwing me following error

failed: [13.82.109.51] (item={u'isdir': False, u'extension': u'.sql', u'isreadonly': False, u'checksum': u'da39a3ee5e6b4b0d3255bfef95601890afd80709', u'size': 0, u'creationtime': 1500861234.9365487, u'filename': u'abcd.sql', u'lastaccesstime': 1500861234.9365487, u'owner': u'BUILTIN\Administrators', u'lastwritetime': 1500861234.9365487, u'islnk': False, u'attributes': u'Archive', u'path': u'C:\Users\atul\Documents\mysql-dev\17-06-2017\abcd.sql', u'isarchive': True, u'ishidden': False, u'isshared': False}) => {"changed": true, "cmd": "mysql -uroot -p devdb < C:\Users\atul\Documents\mysql-dev\17-06-2017\abcd.sql", "delta": "0:00:00.031259", "end": "2017-07-24 06:35:33.243269", "failed": true, "item": {"attributes": "Archive", "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "creationtime": 1500861234.9365487, "extension": ".sql", "filename": "abcd.sql", "isarchive": true, "isdir": false, "ishidden": false, "islnk": false, "isreadonly": false, "isshared": false, "lastaccesstime": 1500861234.9365487, "lastwritetime": 1500861234.9365487, "owner": "BUILTIN\Administrators", "path": "C:\Users\atul\Documents\mysql-dev\17-06-2017\abcd.sql", "size": 0}, "rc": 1, "start": "2017-07-24 06:35:33.212010", "stderr": "", "stderr_lines": [], "stdout": "mysql Ver 14.14 Distrib 5.7.19, for Win64 (x86_64)\r\nCopyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.\r\n\r\nOracle is a registered trademark of Oracle Corporation and/or its\r\naffiliates. Other names may be trademarks of their respective\r\nowners.\r\n\r\nUsage: mysql [OPTIONS] [database]\r\n -?, --help Display this help and exit.\r\n -I, --help Synonym for -?\r\n --auto-rehash
Enable automatic rehashing. One doesn't need to use\r\n
'rehash' to get table and field completion, but startup\r\n
and reconnecting may take a longer time. Disable with\r\n
--disable-auto-rehash.\r\n (Defaults to on; use --skip-auto-rehash to disable.)\r\n -A, --no-auto-rehash \r\n No automatic rehashing. One has to use 'rehash' to get\r\n
table and field completion. This gives a quicker start of\r\n
mysql and disables rehashing on reconnect.\r\n --auto-vertical-output \r\n Automatically switch to vertical output mode if the\r\n result is wider than the terminal width.\r\n -B, --batch Don't use history file. Disable interactive behavior.\r\n (Enables --silent.)\r\n --bind-address=name IP address to bind to.\r\n -b, --binary-as-hex Print binary data as hex\r\n --character-sets-dir=name \r\n
Directory for character set files.\r\n --column-type-info Display column type information.\r\n -c, --comments Preserve comments. Send comments to the server. The\r\n default is --skip-comments (discard comments), enable\r\n with --comments.\r\n -C, --compress Use compression in server/client protocol.\r\n -#, --debug[=#] This is a non-debug version. Catch this and exit.\r\n --debug-check This is a non-debug version. Catch this and exit.\r\n -T, --debug-info This is a non-debug version. Catch this and exit.\r\n -D, --database=name Database to use.\r\n --default-character-set=name \r\n
Set the default character set.\r\n --delimiter=name Delimiter to be used.\r\n --enable-cleartext-plugin \r\n
Enable/disable the clear text authentication plugin.\r\n -e, --execute=name Execute command and quit. (Disables --force and history\r\n file.)\r\n -E, --vertical Print the output of a query (rows) vertically.\r\n -f, --force
Continue even if we get an SQL error.\r\n --histignore=name A colon-separated list of patterns to keep statements\r\n
from getting logged into syslog and mysql history.\r\n -G, --named-commands \r\n Enable named commands. Named commands mean this program's\r\n internal commands; see mysql> help . When enabled, the\r\n
named commands can be used from any line of the query,\r\n
otherwise only from the first line, before an enter.\r\n
Disable with --disable-named-commands. This option is\r\n
disabled by default.\r\n -i, --ignore-spaces Ignore space after function names.\r\n --init-command=name SQL Command to execute when connecting to MySQL server.\r\n Will automatically be re-executed when reconnecting.\r\n --local-infile
Enable/disable LOAD DATA LOCAL INFILE.\r\n -b, --no-beep Turn off beep on error.\r\n -h, --host=name Connect to host.\r\n -H, --html Produce HTML output.\r\n -X, --xml Produce XML output.\r\n --line-numbers Write line numbers for errors.\r\n (Defaults to on; use --skip-line-numbers to disable.)\r\n -L, --skip-line-numbers \r\n Don't write line number for errors.\r\n -n, --unbuffered Flush buffer after each query.\r\n --column-names Write column names in results.\r\n (Defaults to on; use --skip-column-names to disable.)\r\n -N, --skip-column-names \r\n Don't write column names in results.\r\n --sigint-ignore Ignore SIGINT (CTRL-C).\r\n -o, --one-database Ignore statements except those that occur while the\r\n default database is the one named at the command line.\r\n -p, --password[=name] \r\n Password to use when connecting to server. If password is\r\n
not given it's asked from the tty.\r\n -W, --pipe Use named pipes to connect to server.\r\n -P, --port=# Port number to use for connection or 0 for default to, in\r\n
order of preference, my.cnf, $MYSQL_TCP_PORT,\r\n
/etc/services, built-in default (3306).\r\n --prompt=name Set the mysql prompt to this value.\r\n --protocol=name The protocol to use for connection (tcp, socket, pipe,\r\n
memory).\r\n -q, --quick Don't cache result, print it row by row. This may slow\r\n down the server if the output is suspended. Doesn't use\r\n history file.\r\n -r, --raw Write fields without conversion. Used with --batch.\r\n --reconnect Reconnect if the connection is lost. Disable with\r\n --disable-reconnect. This option is enabled by default.\r\n (Defaults to on; use --skip-reconnect to disable.)\r\n -s, --silent Be more silent. Print results with a tab as separator,\r\n
each row on new line.\r\n --shared-memory-base-name=name \r\n
Base name of shared memory.\r\n -S, --socket=name The socket file to use for connection.\r\n --ssl-mode=name SSL connection mode.\r\n --ssl Deprecated. Use --ssl-mode instead.\r\n (Defaults to on; use --skip-ssl to disable.)\r\n --ssl-verify-server-cert \r\n Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead.\r\n --ssl-ca=name CA file in PEM format.\r\n --ssl-capath=name CA directory.\r\n --ssl-cert=name X509 cert in PEM format.\r\n --ssl-cipher=name SSL cipher to use.\r\n --ssl-key=name X509 key in PEM format.\r\n --ssl-crl=name Certificate revocation list.\r\n --ssl-crlpath=name Certificate revocation list path.\r\n --tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1\r\n -t, --table Output in table format.\r\n --tee=name Append everything into outfile. See interactive help (\h)\r\n also. Does not work in batch mode. Disable with\r\n --disable-tee. This option is disabled by default.\r\n -u, --user=name User for login if not current user.\r\n -U, --safe-updates Only allow UPDATE and DELETE that uses keys.\r\n -U, --i-am-a-dummy Synonym for option --safe-updates, -U.\r\n -v, --verbose Write more. (-v -v -v gives the table output format).\r\n -V, --version Output version information and exit.\r\n -w, --wait Wait and retry if connection is down.\r\n --connect-timeout=# Number of seconds before connection timeout.\r\n --max-allowed-packet=# \r\n
The maximum packet length to send to or receive from\r\n
server.\r\n --net-buffer-length=# \r\n The buffer size for TCP/IP and socket communication.\r\n --select-limit=# Automatic limit for SELECT when using --safe-updates.\r\n --max-join-size=# Automatic limit for rows in a join when using\r\n --safe-updates.\r\n --secure-auth Refuse client connecting to server if it uses old\r\n (pre-4.1.1) protocol. Deprecated. Always TRUE\r\n --server-arg=name Send embedded server this as a parameter.\r\n --show-warnings Show warnings after every statement.\r\n -j, --syslog Log filtered interactive commands to syslog. Filtering of\r\n commands depends on the patterns supplied via histignore\r\n
option besides the default patterns.\r\n --plugin-dir=name
Directory for client-side plugins.\r\n --default-auth=name Default authentication client-side plugin to use.\r\n --binary-mode By default, ASCII '\0' is disallowed and '\r\n' is\r\n
translated to '\n'. This switch turns off both features,\r\n
and also turns off parsing of all clientcommands except\r\n
\C and DELIMITER, in non-interactive mode (for input\r\n
piped to mysql or loaded using the 'source' command).\r\n
This is necessary when processing output from mysqlbinlog\r\n
that may contain blobs.\r\n --connect-expired-password \r\n
Notify the server that this client is prepared to handle\r\n
expired password sandbox mode.\r\n\r\nDefault options are read from the following files in the given order:\r\nC:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf C:\Program Files\MySQL\MySQL Server 5.7\my.ini C:\Program Files\MySQL\MySQL Server 5.7\my.cnf C:\Program Files\MySQL\MySQL Server 5.7\bin\my.ini C:\Program Files\MySQL\MySQL Server 5.7\bin\my.cnf \r\nThe following groups are read: mysql client\r\nThe following options may be given as the first argument:\r\n--print-defaults Print the program argument list and exit.\r\n--no-defaults Don't read default options from any option file,\r\n except for login file.\r\n--defaults-file=# Only read default options from the given file #.\r\n--defaults-extra-file=# Read this file after the global files are read.\r\n--defaults-group-suffix=#\r\n
Also read groups with concat(group, suffix)\r\n--login-path=#
Read this path from the login file.\r\n\r\nVariables (--variable-name=value)\r\nand boolean options {FALSE|TRUE} Value (after reading options)\r\n--------------------------------- ----------------------------------------\r\nauto-rehash TRUE\r\nauto-vertical-output FALSE\r\nbind-address
(No default value)\r\nbinary-as-hex
FALSE\r\ncharacter-sets-dir (No default value)\r\ncolumn-type-info FALSE\r\ncomments
FALSE\r\ncompress FALSE\r\ndatabase
(No default value)\r\ndefault-character-set
auto\r\ndelimiter ;\r\nenable-cleartext-plugin FALSE\r\nvertical FALSE\r\nforce
FALSE\r\nhistignore (No default value)\r\nnamed-commands FALSE\r\nignore-spaces
FALSE\r\ninit-command (No default value)\r\nlocal-infile FALSE\r\nno-beep
FALSE\r\nhost (No default value)\r\nhtml FALSE\r\nxml FALSE\r\nline-numbers
TRUE\r\nunbuffered FALSE\r\ncolumn-names
TRUE\r\nsigint-ignore FALSE\r\nport
0\r\nprompt mysql> \r\nquick
FALSE\r\nraw FALSE\r\nreconnect
FALSE\r\nshared-memory-base-name (No default value)\r\nsocket (No default value)\r\nssl TRUE\r\nssl-verify-server-cert FALSE\r\nssl-ca
(No default value)\r\nssl-capath (No default value)\r\nssl-cert (No default value)\r\nssl-cipher (No default value)\r\nssl-key (No default value)\r\nssl-crl (No default value)\r\nssl-crlpath (No default value)\r\ntls-version (No default value)\r\ntable FALSE\r\nuser
root\r\nsafe-updates FALSE\r\ni-am-a-dummy
FALSE\r\nconnect-timeout 0\r\nmax-allowed-packet
16777216\r\nnet-buffer-length 16384\r\nselect-limit
1000\r\nmax-join-size 1000000\r\nsecure-auth
TRUE\r\nshow-warnings FALSE\r\nplugin-dir
(No default value)\r\ndefault-auth (No default value)\r\nbinary-mode
FALSE\r\nconnect-expired-password FALSE\r\n", "stdout_lines": ["mysql Ver 14.14 Distrib 5.7.19, for Win64 (x86_64)", "Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.", "", "Oracle is a registered trademark of Oracle Corporation and/or its", "affiliates. Other names may be trademarks of their respective", "owners.", "", "Usage: mysql [OPTIONS] [database]", " -?, --help
Display this help and exit.", " -I, --help Synonym for -?", " --auto-rehash Enable automatic rehashing. One doesn't need to use", " 'rehash' to get table and field completion, but startup", " and reconnecting may take a longer time. Disable with", "
--disable-auto-rehash.", " (Defaults to on; use --skip-auto-rehash to disable.)", " -A, --no-auto-rehash ", " No automatic rehashing. One has to use 'rehash' to get", "
table and field completion. This gives a quicker start of", "
mysql and disables rehashing on reconnect.", " --auto-vertical-output ", " Automatically switch to vertical output mode if the", " result is wider than the terminal width.", " -B, --batch Don't use history file. Disable interactive behavior.", " (Enables --silent.)", " --bind-address=name IP address to bind to.", " -b, --binary-as-hex Print binary data as hex", " --character-sets-dir=name ", "
Directory for character set files.", " --column-type-info Display column type information.", " -c, --comments Preserve comments. Send comments to the server. The", " default is --skip-comments (discard comments), enable", " with --comments.", " -C, --compress Use compression in server/client protocol.", " -#, --debug[=#] This is a non-debug version. Catch this and exit.", " --debug-check This is a non-debug version. Catch this and exit.", " -T, --debug-info This is a non-debug version. Catch this and exit.", " -D, --database=name Database to use.", " --default-character-set=name ", "
Set the default character set.", " --delimiter=name Delimiter to be used.", " --enable-cleartext-plugin ", "
Enable/disable the clear text authentication plugin.", " -e, --execute=name Execute command and quit. (Disables --force and history", " file.)", " -E, --vertical Print the output of a query (rows) vertically.", " -f, --force
Continue even if we get an SQL error.", " --histignore=name A colon-separated list of patterns to keep statements", "
from getting logged into syslog and mysql history.", " -G, --named-commands ", " Enable named commands. Named commands mean this program's", " internal commands; see mysql> help . When enabled, the", "
named commands can be used from any line of the query,", "
otherwise only from the first line, before an enter.", "
Disable with --disable-named-commands. This option is", "
disabled by default.", " -i, --ignore-spaces Ignore space after function names.", " --init-command=name SQL Command to execute when connecting to MySQL server.", " Will automatically be re-executed when reconnecting.", " --local-infile
Enable/disable LOAD DATA LOCAL INFILE.", " -b, --no-beep Turn off beep on error.", " -h, --host=name Connect to host.", " -H, --html Produce HTML output.", " -X, --xml Produce XML output.", " --line-numbers Write line numbers for errors.", " (Defaults to on; use --skip-line-numbers to disable.)", " -L, --skip-line-numbers ", " Don't write line number for errors.", " -n, --unbuffered Flush buffer after each query.", " --column-names Write column names in results.", " (Defaults to on; use --skip-column-names to disable.)", " -N, --skip-column-names ", " Don't write column names in results.", " --sigint-ignore Ignore SIGINT (CTRL-C).", " -o, --one-database Ignore statements except those that occur while the", " default database is the one named at the command line.", " -p, --password[=name] ", " Password to use when connecting to server. If password is", "
not given it's asked from the tty.", " -W, --pipe Use named pipes to connect to server.", " -P, --port=# Port number to use for connection or 0 for default to, in", "
order of preference, my.cnf, $MYSQL_TCP_PORT,", "
/etc/services, built-in default (3306).", " --prompt=name Set the mysql prompt to this value.", " --protocol=name The protocol to use for connection (tcp, socket, pipe,", "
memory).", " -q, --quick Don't cache result, print it row by row. This may slow", " down the server if the output is suspended. Doesn't use", " history file.", " -r, --raw Write fields without conversion. Used with --batch.", " --reconnect Reconnect if the connection is lost. Disable with", " --disable-reconnect. This option is enabled by default.", " (Defaults to on; use --skip-reconnect to disable.)", " -s, --silent Be more silent. Print results with a tab as separator,", "
each row on new line.", " --shared-memory-base-name=name ", "
Base name of shared memory.", " -S, --socket=name The socket file to use for connection.", " --ssl-mode=name SSL connection mode.", " --ssl Deprecated. Use --ssl-mode instead.", "
(Defaults to on; use --skip-ssl to disable.)", " --ssl-verify-server-cert ", " Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead.", " --ssl-ca=name CA file in PEM format.", " --ssl-capath=name CA directory.", " --ssl-cert=name X509 cert in PEM format.", " --ssl-cipher=name SSL cipher to use.", " --ssl-key=name X509 key in PEM format.", " --ssl-crl=name Certificate revocation list.", " --ssl-crlpath=name Certificate revocation list path.", " --tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1", " -t, --table Output in table format.", " --tee=name Append everything into outfile. See interactive help (\h)", " also. Does not work in batch mode. Disable with", " --disable-tee. This option is disabled by default.", " -u, --user=name User for login if not current user.", " -U, --safe-updates Only allow UPDATE and DELETE that uses keys.", " -U, --i-am-a-dummy Synonym for option --safe-updates, -U.", " -v, --verbose Write more. (-v -v -v gives the table output format).", " -V, --version Output version information and exit.", " -w, --wait Wait and retry if connection is down.", " --connect-timeout=# Number of seconds before connection timeout.", " --max-allowed-packet=# ", "
The maximum packet length to send to or receive from", "
server.", " --net-buffer-length=# ", " The buffer size for TCP/IP and socket communication.", " --select-limit=# Automatic limit for SELECT when using --safe-updates.", " --max-join-size=# Automatic limit for rows in a join when using", " --safe-updates.", " --secure-auth Refuse client connecting to server if it uses old", " (pre-4.1.1) protocol. Deprecated. Always TRUE", " --server-arg=name Send embedded server this as a parameter.", " --show-warnings Show warnings after every statement.", " -j, --syslog Log filtered interactive commands to syslog. Filtering of", " commands depends on the patterns supplied via histignore", "
option besides the default patterns.", " --plugin-dir=name
Directory for client-side plugins.", " --default-auth=name Default authentication client-side plugin to use.", " --binary-mode By default, ASCII '\0' is disallowed and '\r\n' is", "
translated to '\n'. This switch turns off both features,", "
and also turns off parsing of all clientcommands except", "
\C and DELIMITER, in non-interactive mode (for input", "

I am not sure why this happening because everything working fine in Linux instance.

techraf
  • 64,883
  • 27
  • 193
  • 198
Atul Agrawal
  • 1,474
  • 5
  • 22
  • 41

2 Answers2

1

I am not sure why this happening because everything working fine in Linux instance.

mysql_db module does not work on Windows Server target, because it's a regular Python module intended for use on Unix/Linux machines. So you should execute it on a machine that meets the requirements:

Requirements (on host that executes module)

  • python >= 2.7

  • pymssql

There is no need to run it on a machine running MySQL server itself, you can just point to the Windows server in login_host parameter and run the action on Ansible control machine using delegate_to or local_action.


win_command module does not work, because you use shell redirection (< {{ item.path }}):

The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like $env:HOME and operations like <>|, and ; will not work (use the win_shell module if you need these features).

Community
  • 1
  • 1
techraf
  • 64,883
  • 27
  • 193
  • 198
0

Actually i got my query working by using following command

win_shell: "cmd /c --% mysql -h localhost -u{{ db_user }} -p{{ db_pass }}   {{ db_database }} < {{ item.path }}"

Thanks to this link powershell The '<' operator is reserved for future use in Java

Atul Agrawal
  • 1,474
  • 5
  • 22
  • 41