MongoDB Lookup

Hello everybody,

I am learning ansible through its official documentation and videos on youtube, but sadly I can’t get it working with its lookup-mongodb module as described at http://docs.ansible.com/ansible/latest/playbooks_lookups.html#mongodb-lookup
I am using Ubuntu 16.04 based distro (Elementary OS Loki), Python 2.7.12, ansible 2.4.1.0 (installed from ppa:ansible/ansible) and I have also python-pymongo version 3.2-1build1 installed.

When I try to run the example taught in the official documentation (see link above), I got the following error:

app@store:~/ansible_tutorial$ ansible-playbook playbook.yml -vvv
ansible-playbook 2.4.1.0
config file = /home/app/ansible_tutorial/ansible.cfg
configured module search path = [u’/home/app/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
Using /home/app/ansible_tutorial/ansible.cfg as config file
Parsed /home/app/ansible_tutorial/hosts inventory source with ini plugin

PLAYBOOK: playbook.yml **************************************************************************************************************************************************
1 plays in playbook.yml

PLAY [localhost] ********************************************************************************************************************************************************
META: ran handlers

TASK [debug] ************************************************************************************************************************************************************
task path: /home/app/ansible_tutorial/playbook.yml:26
The full traceback is:
Traceback (most recent call last):
File “/usr/lib/python2.7/dist-packages/ansible/executor/task_executor.py”, line 88, in run
items = self._get_loop_items()
File “/usr/lib/python2.7/dist-packages/ansible/executor/task_executor.py”, line 216, in _get_loop_items
items = mylookup.run(terms=loop_terms, variables=self._job_vars, wantlist=True)
File “/usr/lib/python2.7/dist-packages/ansible/plugins/lookup/mongodb.py”, line 210, in run
connection_string = term.get(u’connection_string’, u"mongodb://localhost")
AttributeError: ‘unicode’ object has no attribute ‘get’

fatal: [localhost]: FAILED! => {
“msg”: “Unexpected failure during module execution.”,
“stdout”: “”
}
to retry, use: --limit @/home/app/ansible_tutorial/playbook.retry

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

As far as I can understand, those bold lines are telling me what is going wrong, the connection to the database.
I’ve searched everywhere I could on internet and watched many ansible videos on youtube but I couldn’t find how I can fix it, it must be something very simple.

From the MongoDB side:
I installed it in the same machine, it is running, and it has the local database and startup_log collection, as you may find below:

app@store:~/ansible_tutorial$ mongo
MongoDB shell version: 3.2.17
connecting to: test
Server has startup warnings:
2017-11-04T09:39:25.859-0200 I CONTROL [initandlisten]
2017-11-04T09:39:25.859-0200 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
2017-11-04T09:39:25.859-0200 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2017-11-04T09:39:25.859-0200 I CONTROL [initandlisten]

show dbs
local 0.000GB
use local
switched to db local
show collections
startup_log
db.startup_log.find()
{ “_id” : “store-1509763862598”, “hostname” : “store”, “startTime” : ISODate(“2017-11-04T02:51:02Z”), “startTimeLocal” : “Sat Nov 4 00:51:02.598”, “cmdLine” : { “config” : “/etc/mongod.conf”, “net” : { “bindIp” : “127.0.0.1”, “port” : 27017 }, “storage” : { “dbPath” : “/var/lib/mongodb”, “journal” : { “enabled” : true } }, “systemLog” : { “destination” : “file”, “logAppend” : true, “path” : “/var/log/mongodb/mongod.log”, “quiet” : true } }, “pid” : NumberLong(9044), “buildinfo” : { “version” : “3.2.17”, “gitVersion” : “186656d79574f7dfe0831a7e7821292ab380f667”, “modules” : , “allocator” : “tcmalloc”, “javascriptEngine” : “mozjs”, “sysInfo” : “deprecated”, “versionArray” : [ 3, 2, 17, 0 ], “openssl” : { “running” : “OpenSSL 1.0.2g 1 Mar 2016”, “compiled” : “OpenSSL 1.0.2g 1 Mar 2016” }, “buildEnvironment” : { “distmod” : “ubuntu1604”, “distarch” : “x86_64”, “cc” : “/opt/mongodbtoolchain/v2/bin/gcc: gcc (GCC) 5.4.0”, “ccflags” : “-fno-omit-frame-pointer -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -O2 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-missing-braces -fno-builtin-memcmp”, “cxx” : “/opt/mongodbtoolchain/v2/bin/g++: g++ (GCC) 5.4.0”, “cxxflags” : “-Wnon-virtual-dtor -Woverloaded-virtual -Wno-maybe-uninitialized -std=c++11”, “linkflags” : “-fPIC -pthread -Wl,-z,now -rdynamic -fuse-ld=gold -Wl,-z,noexecstack -Wl,–warn-execstack”, “target_arch” : “x86_64”, “target_os” : “linux” }, “bits” : 64, “debug” : false, “maxBsonObjectSize” : 16777216, “storageEngines” : [ “devnull”, “ephemeralForTest”, “mmapv1”, “wiredTiger” ] } }
{ “_id” : “store-1509795565866”, “hostname” : “store”, “startTime” : ISODate(“2017-11-04T11:39:25Z”), “startTimeLocal” : “Sat Nov 4 09:39:25.866”, “cmdLine” : { “config” : “/etc/mongod.conf”, “net” : { “bindIp” : “127.0.0.1”, “port” : 27017 }, “storage” : { “dbPath” : “/var/lib/mongodb”, “journal” : { “enabled” : true } }, “systemLog” : { “destination” : “file”, “logAppend” : true, “path” : “/var/log/mongodb/mongod.log”, “quiet” : true } }, “pid” : NumberLong(7673), “buildinfo” : { “version” : “3.2.17”, “gitVersion” : “186656d79574f7dfe0831a7e7821292ab380f667”, “modules” : , “allocator” : “tcmalloc”, “javascriptEngine” : “mozjs”, “sysInfo” : “deprecated”, “versionArray” : [ 3, 2, 17, 0 ], “openssl” : { “running” : “OpenSSL 1.0.2g 1 Mar 2016”, “compiled” : “OpenSSL 1.0.2g 1 Mar 2016” }, “buildEnvironment” : { “distmod” : “ubuntu1604”, “distarch” : “x86_64”, “cc” : “/opt/mongodbtoolchain/v2/bin/gcc: gcc (GCC) 5.4.0”, “ccflags” : “-fno-omit-frame-pointer -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -O2 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-missing-braces -fno-builtin-memcmp”, “cxx” : “/opt/mongodbtoolchain/v2/bin/g++: g++ (GCC) 5.4.0”, “cxxflags” : “-Wnon-virtual-dtor -Woverloaded-virtual -Wno-maybe-uninitialized -std=c++11”, “linkflags” : “-fPIC -pthread -Wl,-z,now -rdynamic -fuse-ld=gold -Wl,-z,noexecstack -Wl,–warn-execstack”, “target_arch” : “x86_64”, “target_os” : “linux” }, “bits” : 64, “debug” : false, “maxBsonObjectSize” : 16777216, “storageEngines” : [ “devnull”, “ephemeralForTest”, “mmapv1”, “wiredTiger” ] } }

Does anyone have an idea on how I can get it working?

Many thanks,
Marcelo

Yes, the mongodb lookup plugin is broken in ansible >= 2.4.1.0.

The problem is with this line:

def run(self, terms, variables, **kwargs):

ret =

for term in terms:

When trying to run the example from the documentation, terms holds a dictionary with the mongo parameters and not a list.

I will try to open an issue and submit a pull request for fixing this.

  • David

Though actually the plugin does work when using it with the “lookup” syntax.

The tasks in the plugin docs should be replaced with this:

tasks:

  • set_fact:

entry: “{{ lookup(‘mongodb’, mongodb_parameters) }}”

  • debug: msg=“Mongo has already started with the following PID [{{ entry.pid }}]”

  • David

Has this been addressed in ansible 2.5?
Unable to get this working.

Thanks Carl