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

**URL:** <https://forum.ansible.com/t/ansible-installation-of-mongodb-missing-mongodb-folder-in-var-run/31283>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [September 16, 2019, 7:59am UTC](https://forum.ansible.com/t/ansible-installation-of-mongodb-missing-mongodb-folder-in-var-run/31283 "2019-09-16T07:59:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Thorpe](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Stephen\_Thorpe](https://forum.ansible.com/u/Stephen_Thorpe)\
**Post date:** [September 16, 2019, 7:59am UTC](https://forum.ansible.com/t/ansible-installation-of-mongodb-missing-mongodb-folder-in-var-run/31283/1 "2019-09-16T07:59:18Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 16, 2019, 8:23am UTC](https://forum.ansible.com/t/ansible-installation-of-mongodb-missing-mongodb-folder-in-var-run/31283/2 "2019-09-16T08:23:11Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

> - 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:

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 16, 2019, 9:10am UTC](https://forum.ansible.com/t/ansible-installation-of-mongodb-missing-mongodb-folder-in-var-run/31283/3 "2019-09-16T09:10:55Z")

</div>

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) ... 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  
&nbsp;&nbsp;&nbsp;[https://docs.mongodb.com/manual/reference/configuration-options/](https://docs.mongodb.com/manual/reference/configuration-options/)

2) Fix the service to create the directory. See hints  
&nbsp;&nbsp;&nbsp;[https://github.com/mongodb/mongo/commit/50ca596ace0b1390482408f1b19ffb1f9170cab6](https://github.com/mongodb/mongo/commit/50ca596ace0b1390482408f1b19ffb1f9170cab6)

3) Create the directory on your own at startup.

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Stephen\_Thorpe](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Stephen\_Thorpe](https://forum.ansible.com/u/Stephen_Thorpe)\
**Post date:** [September 16, 2019, 9:34am UTC](https://forum.ansible.com/t/ansible-installation-of-mongodb-missing-mongodb-folder-in-var-run/31283/4 "2019-09-16T09:34:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 16, 2019, 9:46am UTC](https://forum.ansible.com/t/ansible-installation-of-mongodb-missing-mongodb-folder-in-var-run/31283/5 "2019-09-16T09:46:03Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 16, 2019, 11:13am UTC](https://forum.ansible.com/t/ansible-installation-of-mongodb-missing-mongodb-folder-in-var-run/31283/6 "2019-09-16T11:13:17Z")

</div>

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/](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](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

&nbsp;&nbsp;&nbsp;&nbsp;dpkg -l | grep mongo

See what files are installed by the package

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apt-file list mongodb-org

Examine the files and see what services are active

&nbsp;&nbsp;&nbsp;&nbsp;/etc/init.d/mongodb  
&nbsp;&nbsp;&nbsp;&nbsp;/etc/mongodb.conf  
&nbsp;&nbsp;&nbsp;&nbsp;/lib/systemd/system/mongodb.service

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

&nbsp;&nbsp;&nbsp;&nbsp;/var/lib/mongodb/mongod.lock

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

&nbsp;&nbsp;&nbsp;&nbsp;$ ls -1 /var/run/mongodb/  
&nbsp;&nbsp;&nbsp;&nbsp;mongodb-27017.sock

init.d shows

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

If you think it's MongoDB problem proceed there.

HTH, Cheers,

&nbsp;&nbsp;-vlado
