Ansible installation of MongoDB - missing mongodb folder in /var/run

26 views
Skip to first unread message

Stephen Thorpe

unread,
Sep 16, 2019, 3:59:18 AM9/16/19
to Ansible Project
Hi,

I'm deploying MongoDB to a number of servers using the script below.  Servers are Ubuntu 18.0.4.   I have an issue where the installation fails to create a mongo folder in /var/run/ and thus can't start the service (No place to put the PID file)
I modified the Ansible script to create the /var/run/mongodb folder with the correct permissions, so this installs MongoDB and starts it, but after a reboot the MongoDB folder is removed (because var/run is not persistent)

Is this an issue with how I'm installing mongo via Ansible?  



- hosts: mongo
  become: true
  gather_facts: true
  vars:
    ################# Mongo #################
    mongodb_db_path: '/data/db'
    mongodb_log_path: '/data/log'

  tasks:

################################################################
# Install/Setup Mongo
################################################################
   - name: Add MongoDB public GPG key to the apt repo
     apt_key:
       url: '{{ mongo_key_url }}'
       state: present

   - name: Add MongoDB Repo
     apt_repository:
       repo: '{{ mongo_repo_url }}'
       state: present

   - name: Run the equivalent of "apt-get update" as a separate step
     apt:
       update_cache: yes

   - name: Install MongoDB
     apt:
       name: 'mongodb-org'
       state: latest



## Fix
   - name: Create diretory for pid
     file:
       path="/var/run/mongodb"
       owner=mongodb
       group=mongodb
       state=directory


Stefan Hornburg (Racke)

unread,
Sep 16, 2019, 4:23:36 AM9/16/19
to ansible...@googlegroups.com
On 9/16/19 9:59 AM, Stephen Thorpe wrote:
> Hi,
>
> I'm deploying MongoDB to a number of servers using the script below.  Servers are Ubuntu 18.0.4.   I have an issue where
> the installation fails to create a mongo folder in /var/run/ and thus can't start the service (No place to put the PID file)
> I modified the Ansible script to create the /var/run/mongodb folder with the correct permissions, so this installs
> MongoDB and starts it, but after a reboot the MongoDB folder is removed (because var/run is not persistent)
>
> Is this an issue with how I'm installing mongo via Ansible? 

Hello Stephen,

I would expect that the package installed via APT takes care of the PID file and /run subdirectory management.

Regards
Racke
->      file:
>        path="/var/run/mongodb"
>        owner=mongodb
>        group=mongodb
>        state=directory
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/e80f511f-2c26-40cf-a130-9c6d50d809d0%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/e80f511f-2c26-40cf-a130-9c6d50d809d0%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Vladimir Botka

unread,
Sep 16, 2019, 5:11:12 AM9/16/19
to Stephen Thorpe, ansible...@googlegroups.com
Hi,

On Mon, 16 Sep 2019 00:59:18 -0700 (PDT)
Stephen Thorpe <stephen....@gmail.com> wrote:

> I'm deploying MongoDB to a number of servers using the script below.
> Servers are Ubuntu 18.0.4. I have an issue where the installation fails
> to create a mongo folder in /var/run/ and thus can't start the service (No
> place to put the PID file) ... but after a reboot the MongoDB folder is
> removed (because var/run is not persistent)
> [...]
> Is this an issue with how I'm installing mongo via Ansible?

No. It's not Ansible issue at all. MongoDB doesn't care about the directory.
There are more options:

1) Put "pidfile" to persistent location
https://docs.mongodb.com/manual/reference/configuration-options/

2) Fix the service to create the directory. See hints
https://github.com/mongodb/mongo/commit/50ca596ace0b1390482408f1b19ffb1f9170cab6

3) Create the directory on your own at startup.

Cheers,

-vlado

Stephen Thorpe

unread,
Sep 16, 2019, 5:34:47 AM9/16/19
to Ansible Project
Hi Vladimir,

If I install Mongo manually  apt-get install mongodb-org if install fine and the /var/run/mongodb folder is created every reboot
it's only when I install via Ansible then it doesn't.

e.g. a manual install also creates a mongodb file in /etc/init.d  the Ansible install does not

I have worked round the problem with your option 3.
I have created a file in /urs/lib/tmpfiles.d called mongo.conf contents of:

d /var/run/mongodb 0755 mongodb mongodb -

This creates the mongodb directory every reboot.

It does seem weird that the installs differs via Ansible vs manual 

Regards,
Steve

Dick Visser

unread,
Sep 16, 2019, 5:46:24 AM9/16/19
to ansible...@googlegroups.com
On Mon, 16 Sep 2019 at 11:34, Stephen Thorpe <stephen....@gmail.com> wrote:
>
> Hi Vladimir,
>
> If I install Mongo manually apt-get install mongodb-org if install fine and the /var/run/mongodb folder is created every reboot
> it's only when I install via Ansible then it doesn't.
...
> It does seem weird that the installs differs via Ansible vs manual

Yes, I would suggest focussing on fixing that, rather than "working around" it.
There should be no difference between apt-get install package and the
corresponding ansible apt task.

--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Vladimir Botka

unread,
Sep 16, 2019, 7:13:29 AM9/16/19
to Stephen Thorpe, ansible...@googlegroups.com
Hi Stephen,

> > > I'm deploying MongoDB to a number of servers using the script below.
> > > Servers are Ubuntu 18.0.4. I have an issue where the installation
> > > fails
> > > to create a mongo folder in /var/run/ and thus can't start the service
> > > (No
> > > place to put the PID file) ... but after a reboot the MongoDB folder is
> > > removed (because var/run is not persistent)
> > > [...]
> > > Is this an issue with how I'm installing mongo via Ansible?

> > No. It's not Ansible issue at all. MongoDB doesn't care about the
> > directory.
> > There are more options:
> > 1) Put "pidfile" to persistent location
> > https://docs.mongodb.com/manual/reference/configuration-options/
> > 2) Fix the service to create the directory. See hints
> > https://github.com/mongodb/mongo/commit/50ca596ace0b1390482408f1b19ffb1f9170cab6
> > 3) Create the directory on your own at startup.

> If I install Mongo manually apt-get install mongodb-org if install fine
> and the /var/run/mongodb folder is created every reboot
> it's only when I install via Ansible then it doesn't.
>
> e.g. a manual install also creates a mongodb file in /etc/init.d the
> Ansible install does not
>
> I have worked round the problem with your option 3.
> I have created a file in /urs/lib/tmpfiles.d called mongo.conf contents of:
>
> d /var/run/mongodb 0755 mongodb mongodb -
>
> This creates the mongodb directory every reboot.
>
> It does seem weird that the installs differs via Ansible vs manual

There shouldn't be any difference in installing packages manually and with
Ansible. If you're sure you've found a difference open an issue with the
Ansible module.


OFF-TOPIC: MongoDB. Just to close this issue here.

You've configured the repo and installed "mongodb-org" which is maintained
and supported by MongoDB Inc (they claim it's for Ubuntu). Standard Ubuntu
18.04 does not include it.

If you want to find the problem take a look what packages are installed

dpkg -l | grep mongo

See what files are installed by the package

apt-file list mongodb-org

Examine the files and see what services are active

/etc/init.d/mongodb
/etc/mongodb.conf
/lib/systemd/system/mongodb.service

FWIW. Standard Ubuntu 18.04 mongodb-server put PID by default into the file

/var/lib/mongodb/mongod.lock

and the directory /var/run/mongodb lists the socket

$ ls -1 /var/run/mongodb/
mongodb-27017.sock

init.d shows

$ grep RUNDIR /etc/init.d/mongodb
RUNDIR=/run/mongodb
PIDFILE=$RUNDIR/$NAME.pid
DAEMON_OPTS=${DAEMON_OPTS:-"--unixSocketPrefix=$RUNDIR --config $CONF
run"} test -e "$RUNDIR" || install -m 755 -o mongodb -g mongodb -d
"$RUNDIR"

If you think it's MongoDB problem proceed there.

HTH, Cheers,

-vlado
Reply all
Reply to author
Forward
0 new messages