forked from CCCHH/ansible-infra
		
	add postgresql role for ens. psql and opt. some dbs and users are set up
Add postgresql role for ensuring postgresql is installed. Furthermore the role optionally takes some basic configuration to ensure databases with their owners and users are set up as specified. This is a requirement for a new netbox role.
This commit is contained in:
		
					parent
					
						
							
								fc24bfff5d
							
						
					
				
			
			
				commit
				
					
						9662995377
					
				
			
		
					 4 changed files with 95 additions and 0 deletions
				
			
		
							
								
								
									
										37
									
								
								roles/postgresql/README.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								roles/postgresql/README.md
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,37 @@
 | 
			
		|||
# Role `postgresql`
 | 
			
		||||
 | 
			
		||||
Ensures `postgresql` is installed by installing the distributions package.  
 | 
			
		||||
Also ensures the optionally given databases and users are set up as specified.
 | 
			
		||||
 | 
			
		||||
## Supported Distributions
 | 
			
		||||
 | 
			
		||||
Should work on Debian-based distributions.
 | 
			
		||||
 | 
			
		||||
## Required Arguments
 | 
			
		||||
 | 
			
		||||
None.
 | 
			
		||||
 | 
			
		||||
## Optional Arguments
 | 
			
		||||
 | 
			
		||||
- `postgresql__dbs`: List of databases with their owner to ensure are set up.  
 | 
			
		||||
- `postgresql__dbs.*.name`: Name of the database.
 | 
			
		||||
- `postgresql__dbs.*.owner`: Owner of the database.
 | 
			
		||||
- `postgresql__users`: List of users to ensure are set up.
 | 
			
		||||
- `postgresql__users.*.name`: Name of the user.
 | 
			
		||||
- `postgresql__users.*.password`: Optional password for the user.
 | 
			
		||||
   If left unset, the user will have no password set, but can still connect using [peer authentication](https://www.postgresql.org/docs/current/auth-peer.html) on the local system.
 | 
			
		||||
   (Peer authentication works when a password is set as well.)
 | 
			
		||||
 | 
			
		||||
## Example Arguments
 | 
			
		||||
 | 
			
		||||
```yaml
 | 
			
		||||
postgresql__dbs:
 | 
			
		||||
  - name: netbox
 | 
			
		||||
    owner: netbox
 | 
			
		||||
  - name: foo
 | 
			
		||||
    owner: bar
 | 
			
		||||
postgresql__users:
 | 
			
		||||
  - name: netbox
 | 
			
		||||
    password: super_secret
 | 
			
		||||
  - name: bar
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										2
									
								
								roles/postgresql/defaults/main.yaml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								roles/postgresql/defaults/main.yaml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
postgresql__dbs: [ ]
 | 
			
		||||
postgresql__users: [ ]
 | 
			
		||||
							
								
								
									
										28
									
								
								roles/postgresql/meta/argument_specs.yaml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								roles/postgresql/meta/argument_specs.yaml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,28 @@
 | 
			
		|||
argument_specs:
 | 
			
		||||
  main:
 | 
			
		||||
    options:
 | 
			
		||||
      postgresql__dbs:
 | 
			
		||||
        type: list
 | 
			
		||||
        elements: dict
 | 
			
		||||
        required: false
 | 
			
		||||
        default: [ ]
 | 
			
		||||
        options:
 | 
			
		||||
          name:
 | 
			
		||||
            type: str
 | 
			
		||||
            required: true
 | 
			
		||||
          owner:
 | 
			
		||||
            type: str
 | 
			
		||||
            required: true
 | 
			
		||||
      postgresql__users:
 | 
			
		||||
        type: list
 | 
			
		||||
        elements: dict
 | 
			
		||||
        required: false
 | 
			
		||||
        default: [ ]
 | 
			
		||||
        options:
 | 
			
		||||
          name:
 | 
			
		||||
            type: str
 | 
			
		||||
            required: true
 | 
			
		||||
          password:
 | 
			
		||||
            type: str
 | 
			
		||||
            required: false
 | 
			
		||||
            default: ""
 | 
			
		||||
							
								
								
									
										28
									
								
								roles/postgresql/tasks/main.yaml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								roles/postgresql/tasks/main.yaml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,28 @@
 | 
			
		|||
- name: Ensure postgresql is installed
 | 
			
		||||
  ansible.builtin.apt:
 | 
			
		||||
    name:
 | 
			
		||||
      - postgresql
 | 
			
		||||
  become: true
 | 
			
		||||
 | 
			
		||||
- name: Ensure Python library for community.postgresql is installed if needed
 | 
			
		||||
  ansible.builtin.apt:
 | 
			
		||||
    name:
 | 
			
		||||
      - python3-psycopg
 | 
			
		||||
  become: true
 | 
			
		||||
  when: postgresql__dbs != [ ] or postgresql__users != [ ]
 | 
			
		||||
 | 
			
		||||
- name: Ensure users
 | 
			
		||||
  community.postgresql.postgresql_user:
 | 
			
		||||
    name: "{{ item.name }}"
 | 
			
		||||
    password: "{{ item.password | default('') }}"
 | 
			
		||||
  become: true
 | 
			
		||||
  become_user: postgres
 | 
			
		||||
  loop: "{{ postgresql__users }}"
 | 
			
		||||
 | 
			
		||||
- name: Ensure dbs with owners
 | 
			
		||||
  community.postgresql.postgresql_db:
 | 
			
		||||
    name: "{{ item.name }}"
 | 
			
		||||
    owner: "{{ item.owner }}"
 | 
			
		||||
  become: true
 | 
			
		||||
  become_user: postgres
 | 
			
		||||
  loop: "{{ postgresql__dbs }}"
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue