aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/main.yml
blob: 3224833a0d383ead1545639b8593f327f15f798d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---
- name: Bring up docker containers
  hosts: localhost
  vars: &common_vars
    postgres_version: 10
    postgres_barman_directory: 'test-postgres-10'
    postgres_barman_server: postgres_barman
    barman_rsync_password: "secret_rsync"
    barman_rsync_allowed_hosts: 172.17.0.0/24
    postgres_allowed_hosts:
      - user: all
        range: 172.17.0.0/24
    postgres_replication_hosts:
      - user: replicator
        range: 172.17.0.0/24
    inventory:
      - name: "{{ postgres_barman_server }}"
        image: "python:3.7-stretch"
      - name: postgres_one
        image: "python:3.7-stretch"
        links:
          - "{{ postgres_barman_server }}"
      - name: postgres_two
        image: "python:3.7-stretch"
        links:
          - "{{ postgres_barman_server }}"
          - postgres_one
      - name: postgres_three
        image: "python:3.7-stretch"
        links:
          - "{{ postgres_barman_server }}"
          - postgres_one
  roles:
    - role: provision_docker
      provision_docker_inventory: "{{inventory}}"
      provision_docker_use_docker_connection: true
  tasks:
    - name: Group primary
      add_host:
        hostname: "{{item}}"
        groups: primary
        <<: *common_vars
        postgres_clusters:
          - version: "{{ postgres_version }}"
            name: 'main'
            port: 5432
            fsync_enabled: True
            checksums: False
            archive_enabled: True
            barman_directory: "{{ postgres_barman_directory }}"
            users:
              - username: barman
                password: "secret_cocktail"
                permissions: 'SUPERUSER'
              - username: replicator
                password: "secret_repli"
                permissions: 'REPLICATION'

      with_items:
        - postgres_one
      tags: provision_docker

    - name: Group secondaries
      add_host:
        hostname: "{{item}}"
        groups: secondary
        <<: *common_vars
        postgres_clusters:
          - version: "{{ postgres_version }}"
            name: 'main'
            port: 5432
            fsync_enabled: True
            checksums: False
            archive_enabled: True
            barman_directory: "{{ postgres_barman_directory }}"
            primary:
              host: postgres_one
              port: 5432
              restore_directory: "{{ postgres_barman_directory }}"
              replication_user: "replicator"
              replication_password: "secret_repli"

      with_items:
        - postgres_two
        - postgres_three
      tags: provision_docker

    - name: Group db-all
      add_host:
        hostname: "{{item}}"
        groups: db-all
      with_items:
        - postgres_one
        - postgres_two
        - postgres_three
      tags: provision_docker

    - name: Group barman
      add_host:
        hostname: "{{item}}"
        groups: barman
        <<: *common_vars
        barman_databases:
          - name: "{{ postgres_barman_directory }}"
            description: 'Main database'
            primary_host: postgres_one
            primary_barman_password: "secret_cocktail"
            backup_method: rsync
            ansible_group: db-all
            retention_policy: 'RECOVERY WINDOW OF 7 DAYS'
      with_items:
        - postgres_barman
      tags: provision_docker

- name: Test installing Barman role
  hosts: barman
  connection: docker
  vars: {}
  pre_tasks: &pre_tasks
     - name: "Build hosts file"
       shell: 'echo "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }} {{inventory_hostname}}" >> /etc/hosts'
       when: hostvars[inventory_hostname].ansible_default_ipv4.address is defined

     - debug: msg="Running on host {{inventory_hostname}} ({{ hostvars[inventory_hostname].ansible_default_ipv4.address }})"
  roles:
    - { role: barman, tags: ['barman'] }

- name: Test installing Postgresql role
  hosts: primary:secondary
  connection: docker
  vars:
    postgres_listen_addresses: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}"
  pre_tasks: *pre_tasks
  roles:
    - { role: ansible-postgresql-role, tags: ['postgres'] }
  tasks:
    - name: Start postgres cluster
      service: name=postgresql state=started

    - name: Test connection to Postgres instances.
      become: true
      become_user: postgres
      become_method: su
      command: psql --port=5432 --command='select NOW()'


- name: Test running Postgresql role when database is started
  connection: docker
  hosts: primary:secondary
  vars:
    postgres_listen_addresses: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}"
  pre_tasks: *pre_tasks
  roles:
    - { role: ansible-postgresql-role, tags: ['postgres'] }