我正在与一个现有的 ansible MongoDB 角色作斗争,我正在使用 ansible-molecule 进行更新。包含任务/分子文件的完整角色文件夹被简化并上传到gofile。可以下载和模拟。它没有病毒,md5sum 是:6533724b40af534cc64ee0d6b9369943
. 以下是有关我的环境的一些详细信息:
Python 3.8.2
ansible==2.9.10
ansible-lint==4.2.0
molecule==3.0.5
molecule-vagrant==0.3
pytest==5.4.3
testinfra==5.2.1
我在 VirtualBox 上使用 vagrant,目标操作系统是 Ubuntu xenial 和 CentOS 7,用于特定于 mongo 3.6 的安装:
bento/centos-7 (virtualbox, 202006.16.0)
bento/ubuntu-16.04 (virtualbox, 202002.04.0)
本质上,该角色通常能够在两个操作系统上安装 MongoDB。有几个 shell 命令可以检查实例的版本或是否启用了身份验证,或者它是否是副本集成员。其中之一如下:
- name: Checking for mongo version
shell: >
`which mongo` --port {{ mongofamily_server_port | default(27017) }} --eval 'db.version()' $1 | grep 'MongoDB server version' | grep '[0-9]' -o | head -1
register: sreg_mongo_version_info
- debug: var=sreg_mongo_version_info
下面的输出表明这适用于centos,但不适用于ubuntu
molecule converge -s standalone_with_auth
--> Test matrix
└── standalone_with_auth
├── dependency
├── create
├── prepare
└── converge
--> Scenario: 'standalone_with_auth'
--> Action: 'dependency'
Skipping, missing the requirements file.
Skipping, missing the requirements file.
--> Scenario: 'standalone_with_auth'
--> Action: 'create'
Skipping, instances already created.
--> Scenario: 'standalone_with_auth'
--> Action: 'prepare'
Skipping, instances already prepared.
--> Scenario: 'standalone_with_auth'
--> Action: 'converge'
playbook.yml was deprecated, rename it to converge.yml
playbook.yml was deprecated, rename it to converge.yml
PLAY [Converge] ****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [centos]
ok: [ubuntu]
TASK [util : include_tasks] ****************************************************
skipping: [centos]
skipping: [ubuntu]
TASK [util : include_tasks] ****************************************************
skipping: [centos]
skipping: [ubuntu]
TASK [util : include_tasks] ****************************************************
skipping: [centos]
skipping: [ubuntu]
TASK [util : include_tasks] ****************************************************
skipping: [centos]
skipping: [ubuntu]
TASK [util : include_tasks] ****************************************************
included: /Users/joseph/Engineering/Infrastructure_as_code_global/ansible_roles/util/tasks/os.yml for centos, ubuntu
TASK [util : Fixing the LC_TYPE Issue] *****************************************
changed: [ubuntu] => (item={'dest': '/etc/environment', 'line': 'LANG="en_US.utf-8"', 'backup': 'yes'})
changed: [centos] => (item={'dest': '/etc/environment', 'line': 'LANG="en_US.utf-8"', 'backup': 'yes'})
changed: [ubuntu] => (item={'dest': '/etc/environment', 'line': 'LC_ALL="en_US.utf-8"', 'backup': 'yes'})
changed: [centos] => (item={'dest': '/etc/environment', 'line': 'LC_ALL="en_US.utf-8"', 'backup': 'yes'})
changed: [ubuntu] => (item={'dest': '/etc/environment', 'line': 'LC_CTYPE="en_US.UTF-8"', 'backup': 'yes'})
changed: [centos] => (item={'dest': '/etc/environment', 'line': 'LC_CTYPE="en_US.UTF-8"', 'backup': 'yes'})
TASK [util : Creating or Editing a file using lineinfile module] ***************
skipping: [centos]
skipping: [ubuntu]
TASK [mongo_family : Using OS Specific Install file] ***************************
included: /Users/joseph/Engineering/Infrastructure_as_code_global/ansible_roles/mongo_family/tasks/install-RedHat.yml for centos
included: /Users/joseph/Engineering/Infrastructure_as_code_global/ansible_roles/mongo_family/tasks/install-Debian.yml for ubuntu
TASK [mongo_family : Includinng the Repository Manager] ************************
included: /Users/joseph/Engineering/Infrastructure_as_code_global/ansible_roles/mongo_family/tasks/repository.yml for centos
TASK [mongo_family : Adding RedHat family repo] ********************************
changed: [centos]
TASK [mongo_family : Adding Debian Family GPG key] *****************************
skipping: [centos]
TASK [mongo_family : Adding Debian family repo] ********************************
skipping: [centos]
TASK [mongo_family : Installing Mongo Packages] ********************************
changed: [centos]
TASK [mongo_family : Enable Mongo Service] *************************************
changed: [centos]
TASK [mongo_family : Including Mongo Authorization Task] ***********************
included: /Users/joseph/Engineering/Infrastructure_as_code_global/ansible_roles/mongo_family/tasks/add_security_common.yml for centos
TASK [mongo_family : Including authentication and replication checker] *********
included: /Users/joseph/Engineering/Infrastructure_as_code_global/ansible_roles/mongo_family/tasks/sec_and_repl_checker_common.yml for centos
TASK [mongo_family : Checking for mongo version] *******************************
changed: [centos]
TASK [mongo_family : debug] ****************************************************
ok: [centos] => {
"sreg_mongo_version_info": {
"changed": true,
"cmd": "`which mongo` --port 27017 --eval 'db.version()' $1 | grep 'MongoDB server version' | grep '[0-9]' -o | head -1\n",
"delta": "0:00:00.064069",
"end": "2020-07-13 12:43:00.721671",
"failed": false,
"rc": 0,
"start": "2020-07-13 12:43:00.657602",
"stderr": "",
"stderr_lines": [],
"stdout": "3",
"stdout_lines": [
"3"
]
}
}
TASK [mongo_family : Checking whether authentication is enabled for version below 4] ***
changed: [centos]
TASK [mongo_family : Saving value of register variable for whether auth is enabled mongo below 4] ***
ok: [centos]
TASK [mongo_family : Checking whether authentication is enabled for version above 4] ***
skipping: [centos]
TASK [mongo_family : Saving value of register variable for whether auth is enabled mongo above 4] ***
skipping: [centos]
TASK [mongo_family : Checking whether instance is a Master mongo db withtout auth] ***
skipping: [centos]
TASK [mongo_family : Checkinng whether instance is a member of a replica set withou auth] ***
skipping: [centos]
TASK [mongo_family : Saving value of register variable for whether replica is enabled without auth] ***
skipping: [centos]
TASK [mongo_family : Testing whether instance is a member of a replica set with auth] ***
skipping: [centos]
TASK [mongo_family : Includinng the Repository Manager] ************************
included: /Users/joseph/Engineering/Infrastructure_as_code_global/ansible_roles/mongo_family/tasks/repository.yml for ubuntu
TASK [mongo_family : Adding RedHat family repo] ********************************
skipping: [ubuntu]
TASK [mongo_family : Adding Debian Family GPG key] *****************************
changed: [ubuntu]
TASK [mongo_family : Adding Debian family repo] ********************************
changed: [ubuntu]
TASK [mongo_family : Installing Mongo Packages] ********************************
changed: [ubuntu]
TASK [mongo_family : Enable Mongo Service] *************************************
changed: [ubuntu]
TASK [mongo_family : Including Mongo Authorization Task] ***********************
included: /Users/joseph/Engineering/Infrastructure_as_code_global/ansible_roles/mongo_family/tasks/add_security_common.yml for ubuntu
TASK [mongo_family : Including authentication and replication checker] *********
included: /Users/joseph/Engineering/Infrastructure_as_code_global/ansible_roles/mongo_family/tasks/sec_and_repl_checker_common.yml for ubuntu
TASK [mongo_family : Checking for mongo version] *******************************
changed: [ubuntu]
TASK [mongo_family : debug] ****************************************************
ok: [ubuntu] => {
"sreg_mongo_version_info": {
"changed": true,
"cmd": "`which mongo` --port 27017 --eval 'db.version()' $1 | grep 'MongoDB server version' | grep '[0-9]' -o | head -1\n",
"delta": "0:00:00.152946",
"end": "2020-07-13 12:47:16.735580",
"failed": false,
"rc": 0,
"start": "2020-07-13 12:47:16.582634",
"stderr": "exception: connect failed",
"stderr_lines": [
"exception: connect failed"
],
"stdout": "",
"stdout_lines": []
}
}
TASK [mongo_family : Checking whether authentication is enabled for version below 4] ***
changed: [ubuntu]
TASK [mongo_family : Saving value of register variable for whether auth is enabled mongo below 4] ***
ok: [ubuntu]
TASK [mongo_family : Checking whether authentication is enabled for version above 4] ***
skipping: [ubuntu]
TASK [mongo_family : Saving value of register variable for whether auth is enabled mongo above 4] ***
skipping: [ubuntu]
TASK [mongo_family : Checking whether instance is a Master mongo db withtout auth] ***
skipping: [ubuntu]
TASK [mongo_family : Checkinng whether instance is a member of a replica set withou auth] ***
skipping: [ubuntu]
TASK [mongo_family : Saving value of register variable for whether replica is enabled without auth] ***
skipping: [ubuntu]
TASK [mongo_family : Testing whether instance is a member of a replica set with auth] ***
skipping: [ubuntu]
PLAY RECAP *********************************************************************
centos : ok=14 changed=6 unreachable=0 failed=0 skipped=13 rescued=0 ignored=0
ubuntu : ok=15 changed=7 unreachable=0 failed=0 skipped=12 rescued=0 ignored=0
使用命令的逐字逐字副本对 ubuntu 实例进行了手动测试,并且成功。我在 ubuntu 上对此进行了测试,所以我尝试使用 debian jessie,结果是一样的:在 centos 上工作,而不是在 debian 上,并且从直接命令行工作。所以我反过来责怪分子。因此,我通过调用 playbook 设置了一个带有库存的测试文件夹,以便在 ubuntu 上直接运行 ansible,test/outside_molecule_playbook.yml
但结果也是相同的:
在这一点上,我真的不确定发生了什么,因为它对我来说并没有真正加起来。我想知道你们中是否有人有同样的经历以及如何解决的。我实际上已经尝试将环境路径添加到剧本,但这也不起作用。如果没有它,该剧本对于 CentOS 已经很有效了。
所以现在我对它发生的原因以及如何解决它一无所知。
提前致谢。
更新1:我尝试了@β.εηοιτ.βε 的建议,因此命令变为:
- name: Checking for mongo version
shell: >
`which mongo` --port {{ mongofamily_server_port | default(27017) }} --eval 'db.version()'
register: sreg_mongo_version_info
TASK [mongo_family : Checking for mongo version] *******************************
fatal: [ubuntu]: FAILED! => {"changed": true, "cmd": "`which mongo` --port 27017 --eval 'db.version()'\n", "delta": "0:00:00.050829", "end": "2020-07-13 17:51:31.765702", "msg": "non-zero return code", "rc": 1, "start": "2020-07-13 17:51:31.714873", "stderr": "exception: connect failed", "stderr_lines": ["exception: connect failed"], "stdout": "MongoDB shell version v3.6.18\nconnecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb\n2020-07-13T17:51:31.757+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused\n2020-07-13T17:51:31.762+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :\nconnect@src/mongo/shell/mongo.js:263:13\n@(connect):1:6", "stdout_lines": ["MongoDB shell version v3.6.18", "connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb", "2020-07-13T17:51:31.757+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused", "2020-07-13T17:51:31.762+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :", "connect@src/mongo/shell/mongo.js:263:13", "@(connect):1:6"]}
我更加困惑
更新 2
- name: Checking for mongo version
shell: >
$(which mongo) --port {{ mongofamily_server_port | default(27017) }} --eval 'db.version()'
register: sreg_mongo_version_info
TASK [mongo_family : Checking for mongo version] *******************************
fatal: [ubuntu]: FAILED! => {"changed": true, "cmd": "$(which mongo) --port 27017 --eval 'db.version()'\n", "delta": "0:00:00.045356", "end": "2020-07-13 20:48:26.727663", "msg": "non-zero return code", "rc": 1, "start": "2020-07-13 20:48:26.682307", "stderr": "exception: connect failed", "stderr_lines": ["exception: connect failed"], "stdout": "MongoDB shell version v3.6.18\nconnecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb\n2020-07-13T20:48:26.724+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused\n2020-07-13T20:48:26.724+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :\nconnect@src/mongo/shell/mongo.js:263:13\n@(connect):1:6", "stdout_lines": ["MongoDB shell version v3.6.18", "connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb", "2020-07-13T20:48:26.724+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused", "2020-07-13T20:48:26.724+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :", "connect@src/mongo/shell/mongo.js:263:13", "@(connect):1:6"]}
更新 3
我在文件夹中编写了一个名为 check_mongo_version.sh 的小 shell 脚本:
#!/bin/sh
$(which mongo) --port $1 --eval 'db.hostInfo()'
任务变为:
- name: Checking for mongo version
script: "files/check_mongo_version.sh {{ mongofamily_server_port | default(27017) }}"
register: sreg_mongo_version_info
错误与Error: couldn't connect to server 127.0.0.1:27017