I am learning ansible on my local vagrant vm machine and I am setting up mongodb using ansible and I keep getting the error:
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: 'Collection' object is not callable. If you meant to call the 'authenticate' method on a 'Database' object it is failing because no such method exists.
fatal: [192.168.56.11]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 192.168.56.11 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File \"/home/vagrant/.ansible/tmp/ansible-tmp-1640759955.2153473-3678-226245830655855/AnsiballZ_mongodb_user.py\", line 107, in <module>\r\n _ansiballz_main()\r\n File \"/home/vagrant/.ansible/tmp/ansible-tmp-1640759955.2153473-3678-226245830655855/AnsiballZ_mongodb_user.py\", line 99, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/vagrant/.ansible/tmp/ansible-tmp-1640759955.2153473-3678-226245830655855/AnsiballZ_mongodb_user.py\", line 47, in invoke_module\r\n ...
The anisble playbook configuration:
# tasks file for mongodb
- include_vars: secrets.yml
- name: Installing mongodb
apt:
name: mongodb
state: present
- name: Start and enable the engine
service:
name: mongodb
state: started
enabled: yes
- name: Configure Mongodb to accept outside connections
lineinfile:
path: /etc/mongodb.conf
regexp: '^#?bind_ip = 127.0.0.1'
line: '#bind_ip = 127.0.0.1'
notify:
- restart mongodb
- name: Update Ubuntu
apt: update_cache=yes force_apt_get=yes cache_valid_time=3600
- name: Install Python pip
apt:
name: python3-pip
state: present
- name: Install pymongo package
pip:
name: pymongo
state: present
- name: Is authentication enabled?
command: grep "^auth = true" /etc/mongodb.conf
register: auth
ignore_errors: yes
- name: Add an administrator to the db
mongodb_user:
database: admin
name: root
password: "{{ dbpass }}"
roles: root
state: present
when: auth.rc == 1
- name: Enable authentication
lineinfile:
path: /etc/mongodb.conf
regexp: '^#?auth \= true'
line: 'auth = true'
notify:
- restart mongodb
- name: Create the application user
mongodb_user:
login_user: root
login_password: "{{ dbpass }}"
database: "{{ dbname }}"
name: "{{ appdbuser }}"
password: "{{ appdbpass }}"
roles: dbOwner
state: present
This part from my analysis of the error message is the issue:
- name: Create the application user
mongodb_user:
login_user: root
login_password: "{{ dbpass }}"
database: "{{ dbname }}"
name: "{{ appdbuser }}"
password: "{{ appdbpass }}"
roles: dbOwner
state: present
My anisble machine specifications are:
Ubuntu vm:
Linux ubuntu 5.4.0-80-generic #90-Ubuntu SMP Fri Jul 9 22:49:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Ubuntu 20.04.2 LTS
Installed ansible version on Ubuntu vm:
ansible [core 2.12.1]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /home/vagrant/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0]
jinja version = 2.10.1
libyaml = True
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 19 hours ago.
Improve this question
I'm trying to install PostgreSQL and Postgis with Ansible on a Vagrant VM.
But I'm reaching some issues to install and access to PostgreSQL (didn't reach the step of Postgis yet).
My Vagrant VM is an ubuntu/jammy64.
Firstly, I installed PHP on the VM.
Then I try to install PostrgreSQL. In following, my psql task to Ansible:
---
- name: Install
apt:
update_cache: true
name:
- bash
- openssl
- libssl-dev
- libssl-doc
- postgresql
- postgresql-contrib
- libpq-dev
- python3-psycopg2
state: present
- name: Check if initialized
stat:
path: "{{ postgresql_data_dir }}/pg_hba.conf"
register: postgres_data
- name: Empty data dir
file:
path: "{{ postgresql_data_dir }}"
state: absent
when: not postgres_data.stat.exists
- name: Initialize
shell: "{{ postgresql_bin_path }}/initdb -D {{ postgresql_data_dir }}"
become: true
become_user: postgres
when: not postgres_data.stat.exists
- name: Start and enable service
service:
name: postgresql
state: started
enabled: true
- name: Update pg_ident.conf - allow user to auth with postgres
lineinfile:
dest: "/etc/postgresql/{{ postgresql_version }}/main/pg_ident.conf"
insertafter: "# MAPNAME SYSTEM-USERNAME PG-USERNAME"
line: "user_{{ user }} {{ user }} postgres"
- name: Update pg_hba.conf - disable peer for postgres user
lineinfile:
dest: "/etc/postgresql/{{ postgresql_version }}/main/pg_hba.conf"
regexp: "local all postgres peer"
line: "#local all postgres peer"
- name: Update pg_hba.conf - trust all connection
lineinfile:
dest: "/etc/postgresql/{{ postgresql_version }}/main/pg_hba.conf"
regexp: "local all all peer"
line: "local all all trust"
- name: Restart
service:
name: postgresql
state: restarted
enabled: true
- name: "Create database {{ postgresql_db }}"
become: true
become_user: "{{ postgresql_user }}"
postgresql_db:
name: "{{ postgresql_db }}"
state: present
- name: "Create user {{ user }}"
become: yes
become_user: "{{ postgresql_user }}"
postgresql_user:
name: "{{ user }}"
password: "{{ user }}"
state: present
- name: "Grant user {{ user }}"
become: yes
become_user: "{{ postgresql_user }}"
postgresql_privs:
type: database
database: "{{ postgresql_db }}"
roles: "{{ user }}"
grant_option: no
privs: all
notify: psql restart
My vars:
---
postgresql_version: 14
postgresql_bin_path: "/usr/lib/postgresql/{{ postgresql_version }}/bin"
postgresql_data_dir: "/var/lib/postgresql/{{ postgresql_version }}/main"
postgresql_host: localhost
postgresql_port: 5432
postgresql_db: "db_{{ user }}"
postgresql_user: "{{ user }}"
postgresql_password: "{{ user }}"
ansible_ssh_pipelining: true
But when I play the Ansible's playbook I'm getting the following feedback:
TASK [include_role : psql] *****************************************************
TASK [psql : Install] **********************************************************
ok: [192.168.50.50]
TASK [psql : Check if initialized] *********************************************
ok: [192.168.50.50]
TASK [psql : Empty data dir] ***************************************************
skipping: [192.168.50.50]
TASK [psql : Initialize] *******************************************************
skipping: [192.168.50.50]
TASK [psql : Start and enable service] *****************************************
ok: [192.168.50.50]
TASK [psql : Create database db_ojirai] ****************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: Is the server running locally and accepting connections on that socket?
fatal: [192.168.50.50]: FAILED! => {"changed": false, "msg": "unable to connect to database: connection to server on socket \"/var/run/postgresql/.s.PGSQL.5432\" failed: Connection refused\n\tIs the server running locally and accepting connections on that socket?\n"}
PLAY RECAP *********************************************************************
192.168.50.50 : ok=14 changed=0 unreachable=0 failed=1 skipped=2 rescued=0 ignored=0
Can you, guys, explain to me where is my mistake, please? Is it my PostgreSQL installation which is wrong?
Thanks for your feedbacks!
Edit:
I try the suggested solution by β.εηοιτ.βε but the message persist. I tried with following process:
vagrant destroy > export vars (suggested in the post) > vagrant up > ansible deploy
export vars (suggested in the post) > vagrant reload > ansible deploy
export vars (suggested in the post) > vagrant destroy > vagrant up > ansible deploy
vagrant destroy > vagrant up > export vars (suggested in the post) > ansible deploy
Im trying to configure postgres with ansible. i have two vms running ubuntu 22.0.4.1 on an internal network. they are happy to use standard ansible commands. however upon using the standard ansible commands I get.
unable to connect to database: connection to server on socket \"/var/run/postgresql/.s.PGSQL.5432\" failed: fatal: peer authentication failed for user \"postgres\"
message.
the log says
LOG: provided user name (postgres) and authenticated user name (boop) do not match
i used the following playbook:
---
- name: Setup
hosts: postgres_primaries
become: true
tasks:
- name: Install dependencies for PostgreSQL
apt:
name: "{{ item.name }}"
update_cache: true
state: latest
with_items:
- { name: bash }
- { name: openssl }
- { name: libssl-dev }
- { name: libssl-doc }
- name: Install PostgreSQL
package:
name: "{{ item.name }}"
update_cache: true
state: present
with_items:
- { name: postgresql }
- { name: postgresql-contrib }
- { name: libpq-dev }
- { name: python3-psycopg2 }
- name: Ensure the PostgreSQL service is running
service: name=postgresql state=started enabled=yes
- name: Daemon-Reload for Postgres if case of config change
systemd:
state: restarted
daemon-reload: yes
name: postgresql
- name: work on database
hosts: postgres_primaries
become_user: postgres
vars_files:
- vars.yml
tasks:
- name: create database
postgresql_user:
name: test1
password: boop
i tried mapping boop to postgres the following user_name map:
postgres boop postgres
i tried editing my pg_hba.conf to a more catch all condition:
local all all peer
this should give me a user but instead the aforementioned error turns up. if i try to add a become: yes to the final task i get a error related to moving files as an unprivileged user.
I am am using ansible to add admin user in mongodb.. I used below playbook but i am getting error. Can someone suggest the solution.. i have also installed pymongo prior to adding user in order to use module. authentication is disabled in mongod.conf and bindIp is set to 0.0.0.0
- hosts: devqa_mongod_single:dwprod_mongod_single
become: yes
vars_files:
- ../../vars/vars_secrets.yaml
vars:
password: "mongoadmin"
mongoAuth: "/usr/bin/mongosh 'mongodb://admin:{{ password | urlencode() }}#localhost:27017/admin?authSource=admin' --norc --quiet"
mongoNoAuth: "/usr/bin/mongosh 'mongodb://localhost:27017/admin' --norc --quiet"
tasks:
# volume config for mongodb
- name: Create a new xfs primary partition
community.general.parted:
device: /dev/nvme1n1
number: 1
state: present
fs_type: xfs
label: gpt
- name: Create an xfs filesystem on /dev/nvme1n1
community.general.filesystem:
fstype: xfs
state: present
dev: /dev/nvme1n1p1
- name: Create Directory /data/db
ansible.builtin.file:
path: /data/db
state: directory
owner: root
group: root
mode: 0751
- name: Fetch the UUID of /dev/nvme1n1p1
command: blkid -s UUID -o value /dev/nvme1n1p1
changed_when: false
register: blkid_out
- name: Mount /dev/nvme1n1 by UUID
ansible.posix.mount:
path: /data/db
src: UUID={{ blkid_out.stdout }}
fstype: xfs
opts: "defaults,nofail"
passno: 2
state: mounted
# Installation of mongodb
- name: Install aptitude using apt
apt:
name: aptitude
state: latest
update_cache: yes
- name: Import public key
apt_key:
url: 'https://www.mongodb.org/static/pgp/server-6.0.asc'
state: present
- name: Add repository
apt_repository:
filename: '/etc/apt/sources.list.d/mongodb-org-6.0.list'
repo: 'deb https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/6.0 multiverse'
state: present
update_cache: yes
- name: Install mongoDB
apt:
name: mongodb-org
state: present
update_cache: yes
notify:
- restart mongodb
- name: Recursively change ownership of a /data/db
ansible.builtin.file:
path: /data/db
state: directory
recurse: yes
owner: mongodb
group: mongodb
notify:
- restart mongodb
- name: Create Directory /var/run/mongodb
ansible.builtin.file:
path: /var/run/mongodb
state: directory
owner: mongodb
group: mongodb
mode: 0751
notify:
- restart mongodb
- name: Ensure mongodb is running and and enabled to start automatically on reboots
service:
name: mongod
enabled: yes
state: started
# Installing pymongo to use community.mongodb.mongodb_user module
- name: "Install PyMongo"
apt:
update_cache: yes
name: "python3-pymongo"
state: "latest"
# copy temorary config file
- name: user_init | set temporary conf
become: yes
timeout: 300
ansible.builtin.copy:
src: ../templates/mongodb/mongod_init.conf.j2
dest: /etc/mongod.conf
owner: root
group: root
mode: '0644'
notify:
- restart mongodb
# Adding root user
- name: Check if authentication is enabled
shell:
cmd: "{{ mongoAuth }} --eval 'db.getMongo()'"
executable: /bin/bash
register: authenticate
failed_when: false
changed_when: false
check_mode: no
- name: Create users
shell:
cmd: "{{ (authenticate.rc == 0) | ternary(mongoAuth, mongoNoAuth) }} --eval '{{ js }}'"
executable: /bin/bash
vars:
js: |
admin = db.getSiblingDB("admin")
{% if authenticate.rc != 0 %}
admin.createUser({ user: "admin", pwd: "{{ password }}", roles: ["root"] })
admin.auth("admin", "{{ password }}")
{% endif %}
notify:
- restart mongodb
# Copy mongod.conf file having auth enabled
- name: copy mongod.conf | set
become: yes
timeout: 300
ansible.builtin.copy:
src: ../templates/mongodb/mongod.conf.j2
dest: /etc/mongod.conf
owner: root
group: root
mode: '0644'
register: mongo_conf_set
notify:
- restart mongodb
- name: Copy mongodb file for log rotation
become: yes
timeout: 300
ansible.builtin.copy:
src: ../templates/mongodb/mongodb
dest: /etc/logrotate.d/mongodb
owner: root
group: root
mode: 0644
- name: Daemon Reload
shell: systemctl daemon-reload
- name: Starting MongoDB service
service:
name: mongod
state: started
handlers:
- name: restart mongodb
service: name=mongod state=restarted
my mongod.conf file on instance
systemLog:
destination: file
logAppend: true
logRotate: reopen
path: /var/log/mongodb/mongod.log
storage:
dbPath: /data/db
journal:
enabled: true
engine: wiredTiger
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled
I create the users manually:
- hosts: all
vars:
mongoAuth: "/usr/bin/mongosh 'mongodb://admin:{{ password | urlencode() }}#localhost:27017/admin?authSource=admin' --norc --quiet"
mongoNoAuth: "/usr/bin/mongosh 'mongodb://localhost:27017/admin' --norc --quiet"
tasks:
- name: Check if authentication is enabled and if user already exists
shell:
cmd: "{{ mongoAuth }} --eval 'db.getMongo()'"
executable: /bin/bash
register: authenticate
failed_when: false
changed_when: false
check_mode: no
- name: Create users
shell:
cmd: "{{ (authenticate.rc == 0) | ternary(mongoAuth, mongoNoAuth) }} --eval '{{ js }}'"
executable: /bin/bash
vars:
js: |
admin = db.getSiblingDB("admin")
{% if authenticate.rc != 0 %}
admin.createUser({ user: "admin", pwd: "{{ password }}", roles: ["root"] })
admin.auth("admin", "{{ password }}")
{% endif %}
// create more users if needed
admin.createUser(...)
I assume you have a wrong configuration setting on the host.
Unable to connect to database: Unknown option directconnection
This doesn't look like an Ansible error to me.
To help you further out, you should disable mongo authentication, and restart mongo. Then, create 3 users, admin, root and userAdminAnyDatabase. Then restart mongo. Here is an Ansible role I've written for MongoDB, so you can take a look there to see how it works.
I'm trying to configure postgresql by ansible in a VPS.
Look for a solution, I tried to change peer for md5 and trust too in the postgre conf.
My role:
- name: Install o Postgresql
become: yes
apt:
name: ['libpq-dev', 'python3-dev', 'postgresql', 'postgresql-contrib']
- name: Install o psycopg2
become: yes
pip:
name: psycopg2-binary
executable: pip3
- name: ensure postgresql is running
service:
name: postgresql
state: started
enabled: yes
- name: ensure database is created
become: true
become_user: postgres
postgresql_db:
name: "{{ db_name }}"
The tasks 1,2,3 is ok. But the task 4 "ensure database is created" I receive this error:
psycopg2.OperationalError: FATAL: role "postgresql" does not exist
My playbook
- hosts: dev
remote_user: develop
roles:
- update_apt
- nginx
- webapp
- postgresql
- git
So I'm trying to create a Postgres Database on my remote server with Ansible, unfortunately I'm getting this error message
TASK [postgresql : Create database with name sola] *****************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
fatal: [some-remote-server]: FAILED! => {
"changed": false
}
MSG:
unable to connect to database: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
This would be my playbook
- name: enable the PostgreSQL package repository
copy:
src: pgdg-96-redhat.repo
dest: /etc/yum.repos.d/pgdg-96-redhat.repo
- name: install additional packages
yum:
name: "{{ item }}"
state: present
with_items:
- "{{ packages }}"
- name: Ensure bash and OpenSSL are the latest version
yum:
name: "{{ item }}"
update_cache: true
state: latest
with_items:
- bash
- openssl
tags: packages
- name: install system packages
yum:
name: "{{ item }}"
state: installed
with_items:
- "{{ packages }}"
become: yes
- name: Install PostgreSQL
yum:
name: "{{ item }}"
update_cache: true
state: installed
with_items:
- postgresql
- postgresql-contrib
- python-psycopg2
tags: packages
become: yes
- name: enabling postgresql services
service:
name: postgresql
state: started
enabled: yes
- name: Create database with name sola
postgresql_db:
name: sola
encoding: 'UTF-8'
lc_collate: 'en_US.UTF-8'
lc_ctype: 'en_US.UTF-8'
template: 'template0'
- name: Ensure database is created
sudo_user: postgres
postgresql_db:
name: dbname
encoding: 'UTF-8'
lc_collate: 'en_US.UTF-8'
lc_ctype: 'en_US.UTF-8'
template: 'template0'
state: present
My suspicions are, that either something went wrong witht the installation process, so that postgres hasn't even been properly installed on the remote server or that I'm not properly enabling and starting the postgres services. Any help is appreciated!