首页 > 解决方案 > 无法使用ansible创建mysql数据库

问题描述

我正在编写 Ansible 剧本以在 MySQL 中创建数据库。我正在使用 Ansible 2.7 版。无法识别问题/错误。我在 CentOS Linux 版本 7.6.1810 (Core) 中的 ansible 主机系统。

我在我的 ansible 控制器机器上安装了 PyMySQL。traget windows 机器上的 MySQL 版本是:mysql Ver 8.0.14 for Win64 on x86_64 (MySQL Community Server - GPL)。

我在下面写了 ansible playbook 代码。

  • 名称:运行远程 PowerShell 脚本主机:赢任务:

    • name: 在 MySQL 中创建数据库 mysql_db: login_user: 用户 login_password: 密码 name: test state: present register: output

    • 调试:输出="{{ output.stdout }}"

    • 调试:输出="{{ output.stderr }}"
    • 调试:var=output.stdout_lines

我的库存文件如下所示:

[win]一些IP

[win:vars] ansible_user=用户名 ansible_password=密码 ansible_connection= ansible_winrm_server_cert_validation=忽略 ansible_winrm_transport= 基本 ansible_port= 5985

我在运行剧本时遇到以下错误:

PLAY [运行远程 PowerShell 脚本]


任务[收集事实] ************************************************ ****************************************************** ******************************************************好的:[18.220.114.161]

任务 [在 MySQL 中创建数据库] ******************************************* ****************************************************** *******************************************致命:[18.220.114.161] : 失败的!=> {"changed": false, "module_stderr": "Exception calling \"Create\" with \"1\" argument(s): \"At line:4 char:21\r\n+ def _ansiballz_main(): \r\n+
~\r\n在 '(' 之后需要一个表达式。\r\nAt line:12 char:27\r\n+ except (AttributeError, OSError):\r\n+
~\r\nMissing argument in parameter列表。\r\n在 line:14 char:7\r\n+
如果 scriptdir 不是 None:\r\n+ ~\r\n在 if 语句中的 'if' 之后缺少 '('。\r\nAt line:21 char :7\r\n+ if sys.version_info < (3,):\r\n+ ~\r\n在 'if' 之后缺少 '('
~\r\n',' 后缺少表达式。\r\nAt line:21 char:25\r\n+ if sys.version_info < (3,):\r\n+ ~\r\n保留 '<' 运算符供将来使用。\r\n在第 23 行 char:32\r\n+
MOD_DESC = ('.py', 'U', imp.PY_SOURCE)\r\n+
~\r\n',' 后缺少表达式。\ r\n在 line:23 char:33\r\n+
MOD_DESC = ('.py', 'U', imp.PY_SOURCE)\r\n+

statement.\r\nAt line:23 char:32\r\n+         MOD_DESC = ('.py', 'U',
imp.PY_SOURCE)\r\n+                                ~\r\nMissing
closing ')' in expression.\r\nAt line:23 char:46\r\n+         MOD_DESC
= ('.py', 'U', imp.PY_SOURCE)\r\n+                                              ~\r\nUnexpected token ')' in expression or statement.\r\nNot all parse
errors were reported.  Correct the reported errors and try
again.\"\r\nAt line:6 char:1\r\n+ $exec_wrapper =
[ScriptBlock]::Create($split_parts[0])\r\n+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    +
CategoryInfo          : NotSpecified: (:) [],
MethodInvocationException\r\n    + FullyQualifiedErrorId :
ParseException\r\n \r\nThe expression after '&' in a pipeline element
produced an object that was not valid. It must result in a command
\r\nname, a script block, or a CommandInfo object.\r\nAt line:7
char:2\r\n+ &$exec_wrapper\r\n+  ~~~~~~~~~~~~~\r\n    + CategoryInfo  
: InvalidOperation: (:) [], RuntimeException\r\n    +
FullyQualifiedErrorId : BadExpression\r\n ", "module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc":
1}
        to retry, use: --limit @/var/lib/awx/projects/runbooks/manage_mysql_database/create_mysql_db.retry

PLAY RECAP
************************************************************************************************************************************************************* IP           : ok=1    changed=0    unreachable=0    failed=1



标签: mysqldatabaseansibleansible-2.x

解决方案


尝试使用 ansible 调用 AWSCLI 命令,这是我在通过 ansible 创建 MySQL rds 时遇到类似问题的解决方案。
前任:

 - name: create rds using cli for decryption
  command: aws rds create-db-instance --db-instance-identifier "{{ StackName }}" --allocated-storage 5 --db-instance-class "{{ db_instance_type }}" --engine mysql --master-username "{{ db_username }}" --master-user-password "{{ db_password }}" --multi-az --storage-encrypted --kms-key-id "{{ kms_id }}" --publicly-accessible --vpc-security-group-ids "{{ SG_Group }}" --db-subnet-group-name "{{ db_subnetgrp }}"

推荐阅读