Thank you for reading this post, don't forget to subscribe!
С помощью данной роли можно будет установить следующие базы данных:
mysql5.5;
mysql5.6;
mysql5.7;
mysql8.0;
mariadb5.5;
mariadb10_2;
mariadb10_3;
mariadb10_4;
mariadb10_5;
postgresql-9.2;
postgresql-9.4;
postgresql-9.5;
postgresql-9.6;
postgresql-10;
postgresql-11;
postgresql-12
приступим:
[root@ansible ~]# mkdir -p /etc/ansible/{playbooks/roles_play,roles/databases/{handlers,tasks,templates}}
добавляем группу
[root@ansible ~]# cat /etc/ansible/hosts
[databases]
192.168.1.150
В данном плейбуке указываем какая база данных нужна, пароль для рута у mysql и postgres у postgresql, а также производится ли установка через proxy:
[root@ansible ~]# cat /etc/ansible/playbooks/roles_play/databases.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
--- - hosts: databases become: true ignore_errors: yes become_method: sudo gather_facts: yes vars: - installdatabase: true # true\false тут указываем нужно ли устанавливать базу данных - database: postgresql-10 # (mysql55; mysql56; mysql57; mysql80; mariadb55; mariadb10_2; mariadb10_3; mariadb10_4; mariadb10_5; postgresql-9.2; postgresql-9.4; postgresql-9.5; postgresql-9.6; postgresql-10; postgresql-11; postgresql-12 ) # тут выбираем версию базы данных - database_pass: G-rG1D6uOOhs # тут указываем пароль (он должен быть сложным так как иначе не применится по политикам безопасности базы) - proxy: false # (true/false) ставим кластер через прокси сервер или с открытым доступом - http_proxy: "http://192.168.1.179:3128" - https_proxy: "http://192.168.1.179:3128" roles: - databases # tasks: # - include_role: # name: name1 # name: name2 |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/handlers/main.yml
[codesyntax lang="php"]
1 2 3 4 5 6 |
- name: restart mysqld service: name=mysqld state=restarted - name: restart mariadb service: name=mariadb state=restarted |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/templates/mariadb-10.2.repo
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 |
# MariaDB 10.2 CentOS repository list - created 2017-12-26 06:22 UTC # http://downloads.mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.2/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/templates/mariadb-10.3.repo
[codesyntax lang="php"]
1 2 3 4 5 6 |
[mariadb] name=MariaDB baseurl=http://yum.mariadb.org/10.3/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/templates/mariadb-10.4.repo
[codesyntax lang="php"]
1 2 3 4 5 6 |
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.4/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/templates/mariadb-10.5.repo
[codesyntax lang="php"]
1 2 3 4 5 6 |
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.5/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/templates/mysql-5.5.repo
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[mysql-connectors-community] name=MySQL Connectors Community baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/ enabled=1 gpgcheck=0 gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-community] name=MySQL Tools Community baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/ enabled=1 gpgcheck=0 gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.5 [mysql55-community] name=MySQL 5.5 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/ enabled=1 gpgcheck=0 gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/templates/mysql-8.0.repo
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[mysql-connectors-community] name=MySQL Connectors Community baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/ enabled=1 gpgcheck=0 gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-community] name=MySQL Tools Community baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/ enabled=1 gpgcheck=0 gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 8.0 [mysql-cluster-8.0-community] name=MySQL Cluster 8.0 Community baseurl=http://repo.mysql.com/yum/mysql-cluster-8.0-community/el/7/$basearch/ enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/templates/pg_hba.conf
[codesyntax lang="php"]
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 |
# PostgreSQL Client Authentication Configuration File # =================================================== # # Refer to the "Client Authentication" section in the PostgreSQL # documentation for a complete description of this file. A short # synopsis follows. # # This file controls: which hosts are allowed to connect, how clients # are authenticated, which PostgreSQL user names they can use, which # databases they can access. Records take one of these forms: # # local DATABASE USER METHOD [OPTIONS] # host DATABASE USER ADDRESS METHOD [OPTIONS] # hostssl DATABASE USER ADDRESS METHOD [OPTIONS] # hostnossl DATABASE USER ADDRESS METHOD [OPTIONS] # # (The uppercase items must be replaced by actual values.) # # The first field is the connection type: "local" is a Unix-domain # socket, "host" is either a plain or SSL-encrypted TCP/IP socket, # "hostssl" is an SSL-encrypted TCP/IP socket, and "hostnossl" is a # plain TCP/IP socket. # # DATABASE can be "all", "sameuser", "samerole", "replication", a # database name, or a comma-separated list thereof. The "all" # keyword does not match "replication". Access to replication # must be enabled in a separate record (see example below). # # USER can be "all", a user name, a group name prefixed with "+", or a # comma-separated list thereof. In both the DATABASE and USER fields # you can also write a file name prefixed with "@" to include names # from a separate file. # # ADDRESS specifies the set of hosts the record matches. It can be a # host name, or it is made up of an IP address and a CIDR mask that is # an integer (between 0 and 32 (IPv4) or 128 (IPv6) inclusive) that # specifies the number of significant bits in the mask. A host name # that starts with a dot (.) matches a suffix of the actual host name. # Alternatively, you can write an IP address and netmask in separate # columns to specify the set of hosts. Instead of a CIDR-address, you # can write "samehost" to match any of the server's own IP addresses, # or "samenet" to match any address in any subnet that the server is # directly connected to. # # METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", # "krb5", "ident", "peer", "pam", "ldap", "radius" or "cert". Note that # "password" sends passwords in clear text; "md5" is preferred since # it sends encrypted passwords. # # OPTIONS are a set of options for the authentication in the format # NAME=VALUE. The available options depend on the different # authentication methods -- refer to the "Client Authentication" # section in the documentation for a list of which options are # available for which authentication methods. # # Database and user names containing spaces, commas, quotes and other # special characters must be quoted. Quoting one of the keywords # "all", "sameuser", "samerole" or "replication" makes the name lose # its special character, and just match a database or username with # that name. # # This file is read on server startup and when the postmaster receives # a SIGHUP signal. If you edit the file on a running system, you have # to SIGHUP the postmaster for the changes to take effect. You can # use "pg_ctl reload" to do that. # Put your actual configuration here # ---------------------------------- # # If you want to allow non-local connections, you need to add more # "host" records. In that case you will also need to make PostgreSQL # listen on a non-local interface via the listen_addresses # configuration parameter, or via the -i or -h command line switches. # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only #local all all peer # IPv4 local connections: host all all 127.0.0.1/32 ident # IPv6 local connections: host all all ::1/128 ident # Allow replication connections from localhost, by a user with the # replication privilege. #local replication postgres peer #host replication postgres 127.0.0.1/32 ident #host replication postgres ::1/128 ident |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/add-proxy.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- name: Add PROXY "{{ http_proxy }}" for install "{{ database }}" lineinfile: dest=/etc/yum.conf state=present regexp="{{ http_proxy }}" insertafter=EOF line="proxy={{ http_proxy }}" ignore_errors: yes - name: PROXY ----- add proxy to /etc/environment blockinfile: dest: /etc/environment block: | export http_proxy="{{ http_proxy }}" export https_proxy="{{ http_proxy }}" state: present |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/check-packpages.yml
[codesyntax lang="php"]
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 |
- name: "Check installed packpages" package_facts: manager: "auto" - name: "if MYSQL installed all OK" debug: msg: "Packpage mysql FOUND and all ok" when: - "'mysql-community-server' in ansible_facts.packages" # - "inventory_hostname in groups['mysql']" - name: "if mariadb-5.5 installed all OK" debug: msg: "Packpage mariadb-5.5 FOUND and all ok" when: - "'mariadb-server' in ansible_facts.packages" # - "inventory_hostname in groups['mysql']" - name: "if mariadb-10.2 installed all OK" debug: msg: "Packpage mariadb-10.2 FOUND and all ok" when: - "'MariaDB-server' in ansible_facts.packages" # - "inventory_hostname in groups['mysql']" - name: "if mariadb-10.3 installed all OK" debug: msg: "Packpage mariadb-10.3 FOUND and all ok" when: - "'MariaDB-server' in ansible_facts.packages" - name: "if mariadb-10.4 installed all OK" debug: msg: "Packpage mariadb-10.4 FOUND and all ok" when: - "'MariaDB-server' in ansible_facts.packages" - name: "if mariadb-10.5 installed all OK" debug: msg: "Packpage mariadb-10.5 FOUND and all ok" when: - "'MariaDB-server' in ansible_facts.packages" - name: "if postgres-9.2 installed all OK" debug: msg: "Packpage postgres-9.2 FOUND and all ok" when: - "'postgresql' in ansible_facts.packages" - name: "if postgres-9.4 installed all OK" debug: msg: "Packpage postgres-9.4 FOUND and all ok" when: - "'postgresql94' in ansible_facts.packages" - name: "if postgres-9.5 installed all OK" debug: msg: "Packpage postgres-9.5 FOUND and all ok" when: - "'postgresql95' in ansible_facts.packages" - name: "if postgres-9.6 installed all OK" debug: msg: "Packpage postgres-9.6 FOUND and all ok" when: - "'postgresql96' in ansible_facts.packages" - name: "if postgres-10 installed all OK" debug: msg: "Packpage postgres-10 FOUND and all ok" when: - "'postgresql10' in ansible_facts.packages" - name: "if postgres-11 installed all OK" debug: msg: "Packpage postgres-11 FOUND and all ok" when: - "'postgresql11' in ansible_facts.packages" - name: "if postgres-12 installed all OK" debug: msg: "Packpage postgres-12 FOUND and all ok" when: - "'postgresql12' in ansible_facts.packages" #- name: "Ansible Print a variable" # debug: # var: ansible_facts.packages |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/delete-proxy.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- name: Add PROXY "{{ http_proxy }}" for install "{{ database }}" lineinfile: dest=/etc/yum.conf state=absent regexp="{{ http_proxy }}" insertafter=EOF line="proxy={{ http_proxy }}" ignore_errors: yes - name: PROXY ----- add proxy to /etc/environment blockinfile: dest: /etc/environment block: | export http_proxy="{{ http_proxy }}" export https_proxy="{{ http_proxy }}" state: absent |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/install-packpages.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 |
- name: install default packages for all host yum: name: "{{ packages }}" state: present vars: packages: - yum-utils - vim - htop |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/main.yml
[codesyntax lang="php"]
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
--- - import_tasks: add-proxy.yml tags: add proxy when: proxy - import_tasks: repo.yml tags: repo - import_tasks: check-packpages.yml tags: check-packpages - import_tasks: install-packpages.yml tags: packpages - import_tasks: mysql-5.5.yml tags: mysql-5.5 when: # - "inventory_hostname in groups['mysql']" - "'mysql-community-server' not in ansible_facts.packages" - "'mariadb-server' not in ansible_facts.packages" - "'MariaDB-server' not in ansible_facts.packages" - "{{ database == 'mysql55' }}" - "{{ installdatabase }}" - import_tasks: mysql-5.6.yml tags: mysql-5.6 when: # - "inventory_hostname in groups['mysql']" - "'mysql-community-server' not in ansible_facts.packages" - "'mariadb-server' not in ansible_facts.packages" - "'MariaDB-server' not in ansible_facts.packages" - "{{ database == 'mysql56' }}" - "{{ installdatabase }}" - import_tasks: mysql-5.7.yml tags: mysql-5.7 when: # - "inventory_hostname in groups['mysql']" - "'mysql-community-server' not in ansible_facts.packages" - "'mariadb-server' not in ansible_facts.packages" - "'MariaDB-server' not in ansible_facts.packages" - "{{ database == 'mysql57' }}" - "{{ installdatabase }}" - import_tasks: mysql-8.0.yml tags: mysql-8.0 when: - "'mysql-community-server' not in ansible_facts.packages" - "'mariadb-server' not in ansible_facts.packages" - "'MariaDB-server' not in ansible_facts.packages" - "{{ database == 'mysql80' }}" - "{{ installdatabase }}" - import_tasks: mariadb-5.5.yml tags: mariadb-5.5 when: # - "inventory_hostname in groups['mysql']" - "'mariadb-server' not in ansible_facts.packages" - "'MariaDB-server' not in ansible_facts.packages" - "'mysql-community-server' not in ansible_facts.packages" - "{{ database == 'mariadb55' }}" - "{{ installdatabase }}" - import_tasks: mariadb-10.2.yml tags: mariadb-10.2 when: # - "inventory_hostname in groups['mysql']" - "'mariadb-server' not in ansible_facts.packages" - "'MariaDB-server' not in ansible_facts.packages" - "'mysql-community-server' not in ansible_facts.packages" - "{{ database == 'mariadb10_2' }}" - "{{ installdatabase }}" - import_tasks: mariadb-10.3.yml tags: mariadb-10.3 when: - "'mariadb-server' not in ansible_facts.packages" - "'MariaDB-server' not in ansible_facts.packages" - "'mysql-community-server' not in ansible_facts.packages" - "{{ database == 'mariadb10_3' }}" - "{{ installdatabase }}" - import_tasks: mariadb-10.4.yml tags: mariadb-10.4 when: - "'mariadb-server' not in ansible_facts.packages" - "'MariaDB-server' not in ansible_facts.packages" - "'mysql-community-server' not in ansible_facts.packages" - "{{ database == 'mariadb10_4' }}" - "{{ installdatabase }}" - import_tasks: mariadb-10.5.yml tags: mariadb-10.5 when: - "'mariadb-server' not in ansible_facts.packages" - "'MariaDB-server' not in ansible_facts.packages" - "'mysql-community-server' not in ansible_facts.packages" - "{{ database == 'mariadb10_5' }}" - "{{ installdatabase }}" - import_tasks: postgresql-9.2.yml tags: postgresql-9.2 when: # - "inventory_hostname in groups['mysql']" - "'postgresql' not in ansible_facts.packages" - "'postgresql94' not in ansible_facts.packages" - "'postgresql95' not in ansible_facts.packages" - "'postgresql96' not in ansible_facts.packages" - "'postgresql10' not in ansible_facts.packages" - "'postgresql11' not in ansible_facts.packages" - "'postgresql12' not in ansible_facts.packages" - "{{ database == 'postgresql-9.2' }}" - "{{ installdatabase }}" - import_tasks: postgresql-9.4.yml tags: postgresql-9.4 when: - "'postgresql' not in ansible_facts.packages" - "'postgresql94' not in ansible_facts.packages" - "'postgresql95' not in ansible_facts.packages" - "'postgresql96' not in ansible_facts.packages" - "'postgresql10' not in ansible_facts.packages" - "'postgresql11' not in ansible_facts.packages" - "'postgresql12' not in ansible_facts.packages" - "{{ database == 'postgresql-9.4' }}" - "{{ installdatabase }}" - import_tasks: postgresql-9.5.yml tags: postgresql-9.5 when: - "'postgresql' not in ansible_facts.packages" - "'postgresql94' not in ansible_facts.packages" - "'postgresql95' not in ansible_facts.packages" - "'postgresql96' not in ansible_facts.packages" - "'postgresql10' not in ansible_facts.packages" - "'postgresql11' not in ansible_facts.packages" - "'postgresql12' not in ansible_facts.packages" - "{{ database == 'postgresql-9.5' }}" - "{{ installdatabase }}" - import_tasks: postgresql-9.6.yml tags: postgresql-9.6 when: - "'postgresql' not in ansible_facts.packages" - "'postgresql94' not in ansible_facts.packages" - "'postgresql95' not in ansible_facts.packages" - "'postgresql96' not in ansible_facts.packages" - "'postgresql10' not in ansible_facts.packages" - "'postgresql11' not in ansible_facts.packages" - "'postgresql12' not in ansible_facts.packages" - "{{ database == 'postgresql-9.6' }}" - "{{ installdatabase }}" - import_tasks: postgresql-10.yml tags: postgresql-10 when: - "'postgresql' not in ansible_facts.packages" - "'postgresql94' not in ansible_facts.packages" - "'postgresql95' not in ansible_facts.packages" - "'postgresql96' not in ansible_facts.packages" - "'postgresql10' not in ansible_facts.packages" - "'postgresql11' not in ansible_facts.packages" - "'postgresql12' not in ansible_facts.packages" - "{{ database == 'postgresql-10' }}" - "{{ installdatabase }}" - import_tasks: postgresql-11.yml tags: postgresql-11 when: - "'postgresql' not in ansible_facts.packages" - "'postgresql94' not in ansible_facts.packages" - "'postgresql95' not in ansible_facts.packages" - "'postgresql96' not in ansible_facts.packages" - "'postgresql10' not in ansible_facts.packages" - "'postgresql11' not in ansible_facts.packages" - "'postgresql12' not in ansible_facts.packages" - "{{ database == 'postgresql-11' }}" - "{{ installdatabase }}" - import_tasks: postgresql-12.yml tags: postgresql-12 when: - "'postgresql' not in ansible_facts.packages" - "'postgresql94' not in ansible_facts.packages" - "'postgresql95' not in ansible_facts.packages" - "'postgresql96' not in ansible_facts.packages" - "'postgresql10' not in ansible_facts.packages" - "'postgresql11' not in ansible_facts.packages" - "'postgresql12' not in ansible_facts.packages" - "{{ database == 'postgresql-12' }}" - "{{ installdatabase }}" - import_tasks: delete-proxy.yml tags: delete proxy when: proxy |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/mariadb-10.2.yml
[codesyntax lang="php"]
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 |
- name: Add repo for mariadb 10.2 template: src=/etc/ansible/roles/databases/templates/mariadb-10.2.repo dest=/etc/yum.repos.d/mariadb-10.2.repo - name: Install mariadb 10.2 yum: name: "{{ packages }}" state: present vars: packages: - MariaDB-server - MariaDB-client - name: Start the mariadb service service: name=mariadb state=started enabled=true - name: Change mariadb root password and keep track in shell: | mysql -uroot -e "SET PASSWORD FOR root@'localhost' = PASSWORD('{{database_pass}}'); flush privileges;" register: change_temp_pass notify: restart mariadb - meta: flush_handlers - debug: var: change_temp_pass |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/mariadb-10.3.yml
[codesyntax lang="php"]
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 |
- name: Add repo for mariadb 10.3 template: src=/etc/ansible/roles/databases/templates/mariadb-10.3.repo dest=/etc/yum.repos.d/mariadb-10.3.repo - name: Install mariadb 10.3 yum: name: "{{ packages }}" state: present vars: packages: - MariaDB-server - MariaDB-client - name: Start the mariadb service service: name=mariadb state=started enabled=true - name: Change mariadb root password and keep track in shell: | mysql -uroot -e "SET PASSWORD FOR root@'localhost' = PASSWORD('{{database_pass}}'); flush privileges;" register: change_temp_pass notify: restart mariadb - meta: flush_handlers - debug: var: change_temp_pass |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/mariadb-10.4.yml
[codesyntax lang="php"]
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 |
- name: Add repo for mariadb 10.4 template: src=/etc/ansible/roles/databases/templates/mariadb-10.4.repo dest=/etc/yum.repos.d/mariadb-10.4.repo - name: Install mariadb 10.4 yum: name: "{{ packages }}" state: present vars: packages: - MariaDB-server - MariaDB-client - name: Start the mariadb service service: name=mariadb state=started enabled=true - name: Change mariadb root password and keep track in shell: | mysql -uroot -e "SET PASSWORD FOR root@'localhost' = PASSWORD('{{database_pass}}'); flush privileges;" register: change_temp_pass notify: restart mariadb - meta: flush_handlers - debug: var: change_temp_pass |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/mariadb-10.5.yml
[codesyntax lang="php"]
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 |
- name: Add repo for mariadb 10.5 template: src=/etc/ansible/roles/databases/templates/mariadb-10.5.repo dest=/etc/yum.repos.d/mariadb-10.5.repo - name: Install mariadb 10.5 yum: name: "{{ packages }}" state: present vars: packages: - MariaDB-server - MariaDB-client - name: Start the mariadb service service: name=mariadb state=started enabled=true - name: Change mariadb root password and keep track in shell: | mysql -uroot -e "SET PASSWORD FOR root@'localhost' = PASSWORD('{{database_pass}}'); flush privileges;" register: change_temp_pass notify: restart mariadb - meta: flush_handlers - debug: var: change_temp_pass |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/mysql-5.5.yml
[codesyntax lang="php"]
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 |
- name: Add repo for mysql 5.5 template: src=/etc/ansible/roles/databases/templates/mysql-5.5.repo dest=/etc/yum.repos.d/mysql-5.5.repo - name: Install MySQL 5.5 yum: name: "{{ packages }}" state: present vars: packages: - mysql-community-server - mysql-community-client - MySQL-python - name: Start the MySQL service service: name=mysqld state=started enabled=true - name: Change mysql root password and keep track in shell: | mysql -uroot -e "SET PASSWORD FOR root@'localhost' = PASSWORD('{{database_pass}}'); flush privileges;" register: change_temp_pass notify: restart mysqld - meta: flush_handlers - debug: var: change_temp_pass |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/mysql-5.6.yml
[codesyntax lang="php"]
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 |
- name: Install MySQL 5.6 repo yum: name: http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm state: present - name: Install MySQL 5.6 yum: name: "{{ packages }}" state: present vars: packages: - mysql-community-server - mysql-community-client - MySQL-python - name: Start the MySQL service service: name=mysqld state=started enabled=true - name: Change mysql root password and keep track in shell: | mysql -uroot --connect-expired-password -e "SET PASSWORD FOR root@'localhost' = PASSWORD('{{database_pass}}'); flush privileges;" register: change_temp_pass notify: restart mysqld - meta: flush_handlers - debug: var: change_temp_pass |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/mysql-5.7.yml
[codesyntax lang="php"]
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 |
- name: Install MySQL 5.7 repo yum: name: http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm state: present - name: Install MySQL 5.7 yum: name: "{{ packages }}" state: present vars: packages: - mysql-community-server - mysql-community-client - MySQL-python - name: Start the MySQL service service: name=mysqld state=started enabled=true - name: Change mysql root password and keep track in shell: | password_match=`awk '/A temporary password is generated for/ {a=$0} END{ print a }' /var/log/mysqld.log | awk '{print $(NF)}'` echo $password_match mysql -uroot -p$password_match --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '{{database_pass}}'; flush privileges; " echo "[client]" user=root password="{{database_pass}}" > /root/.my.cnf args: creates: /root/.my.cnf register: change_temp_pass notify: restart mysqld - meta: flush_handlers - debug: var: change_temp_pass |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/mysql-8.0.yml
[codesyntax lang="php"]
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 |
- name: Install MySQL 8.0 repo yum: name: https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm state: present - name: Install MySQL 8.0 yum: name: "{{ packages }}" state: present vars: packages: - mysql-community-server - mysql-community-client - MySQL-python - name: Start the MySQL service service: name=mysqld state=started enabled=true - name: Change mysql root password and keep track in shell: | password_match=`awk '/A temporary password is generated for/ {a=$0} END{ print a }' /var/log/mysqld.log | awk '{print $(NF)}'` echo $password_match mysql -uroot -p$password_match --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '{{database_pass}}'; flush privileges; " echo "[client]" user=root password="{{database_pass}}" > /root/.my.cnf args: creates: /root/.my.cnf register: change_temp_pass notify: restart mysqld - meta: flush_handlers - debug: var: change_temp_pass |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/postgresql-10.yml
[codesyntax lang="php"]
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 |
- name: Install REPO postgresql-10 yum: name: https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm state: present - name: Install postgresql-10 yum: name: "{{ packages }}" state: present vars: packages: - postgresql10 - postgresql10-server - postgresql10-contrib - postgresql10-libs - name: initialize postgresql command: /usr/pgsql-10/bin/postgresql-10-setup initdb - name: Add pg_hba for postgresql-10 template: src=/etc/ansible/roles/databases/templates/pg_hba.conf dest=/var/lib/pgsql/10/data/pg_hba.conf - name: add peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/10/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: present - name: Start the postgresql-10 service service: name=postgresql-10 state=started enabled=true - name: Change postgres password for user postgres "{{database_pass}}" shell: | sudo -i -u postgres psql -c "ALTER USER postgres PASSWORD '{{database_pass}}';" - name: delete peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/10/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: absent - name: add parameters for work password postgres blockinfile: dest: /var/lib/pgsql/10/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all md5 host all all 0.0.0.0/0 md5 state: present - name: set postgresql.conf access lineinfile: dest: /var/lib/pgsql/10/data/postgresql.conf line: "listen_addresses = '*'" state: present - name: restart postgres service: name=postgresql-10 state=restarted |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/postgresql-11.yml
[codesyntax lang="php"]
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 |
- name: Install REPO postgresql-11 yum: name: https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm state: present - name: Install postgresql-11 yum: name: "{{ packages }}" state: present vars: packages: - postgresql11 - postgresql11-server - postgresql11-contrib - postgresql11-libs - name: initialize postgresql command: /usr/pgsql-11/bin/postgresql-11-setup initdb - name: Add pg_hba for postgresql-11 template: src=/etc/ansible/roles/databases/templates/pg_hba.conf dest=/var/lib/pgsql/11/data/pg_hba.conf - name: add peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/11/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: present - name: Start the postgresql-11 service service: name=postgresql-11 state=started enabled=true - name: Change postgres password for user postgres "{{database_pass}}" shell: | sudo -i -u postgres psql -c "ALTER USER postgres PASSWORD '{{database_pass}}';" - name: delete peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/11/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: absent - name: add parameters for work password postgres blockinfile: dest: /var/lib/pgsql/11/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all md5 host all all 0.0.0.0/0 md5 state: present - name: set postgresql.conf access lineinfile: dest: /var/lib/pgsql/11/data/postgresql.conf line: "listen_addresses = '*'" state: present - name: restart postgres service: name=postgresql-11 state=restarted |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/postgresql-12.yml
[codesyntax lang="php"]
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 |
- name: Install REPO postgresql-12 yum: name: https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm state: present - name: Install postgresql-12 yum: name: "{{ packages }}" state: present vars: packages: - postgresql12 - postgresql12-server - postgresql12-contrib - postgresql12-libs - name: initialize postgresql command: /usr/pgsql-12/bin/postgresql-12-setup initdb - name: Add pg_hba for postgresql-12 template: src=/etc/ansible/roles/databases/templates/pg_hba.conf dest=/var/lib/pgsql/12/data/pg_hba.conf - name: add peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/12/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: present - name: Start the postgresql-12 service service: name=postgresql-12 state=started enabled=true - name: Change postgres password for user postgres "{{database_pass}}" shell: | sudo -i -u postgres psql -c "ALTER USER postgres PASSWORD '{{database_pass}}';" - name: delete peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/12/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: absent - name: add parameters for work password postgres blockinfile: dest: /var/lib/pgsql/12/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all md5 host all all 0.0.0.0/0 md5 state: present - name: set postgresql.conf access lineinfile: dest: /var/lib/pgsql/12/data/postgresql.conf line: "listen_addresses = '*'" state: present - name: restart postgres service: name=postgresql-12 state=restarted |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/postgresql-9.2.yml
[codesyntax lang="php"]
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 |
- name: Install postgresql-9.2 yum: name: "{{ packages }}" state: present vars: packages: - postgresql - postgresql-server - name: initialize postgresql command: postgresql-setup initdb - name: Add pg_hba for postgresql-9.2 template: src=/etc/ansible/roles/databases/templates/pg_hba.conf dest=/var/lib/pgsql/data/pg_hba.conf - name: add peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: present - name: Start the postgresql-9.2 service service: name=postgresql state=started enabled=true - name: Change postgres password for user postgres "{{database_pass}}" shell: | sudo -i -u postgres psql -c "ALTER USER postgres PASSWORD '{{database_pass}}';" - name: delete peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: absent - name: add parameters for work password postgres blockinfile: dest: /var/lib/pgsql/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all md5 host all all 0.0.0.0/0 md5 state: present - name: set postgresql.conf access lineinfile: dest: /var/lib/pgsql/data/postgresql.conf line: "listen_addresses = '*'" state: present - name: restart postgres service: name=postgresql state=restarted |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/postgresql-9.4.yml
[codesyntax lang="php"]
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 |
- name: Install REPO postgresql-9.4 yum: name: https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm state: present - name: Install postgresql-9.4 yum: name: "{{ packages }}" state: present vars: packages: - postgresql94 - postgresql94-server - postgresql94-contrib - postgresql94-libs - name: initialize postgresql command: /usr/pgsql-9.4/bin/postgresql94-setup initdb - name: Add pg_hba for postgresql-9.4 template: src=/etc/ansible/roles/databases/templates/pg_hba.conf dest=/var/lib/pgsql/9.4/data/pg_hba.conf - name: add peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/9.4/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: present - name: Start the postgresql-9.4 service service: name=postgresql-9.4 state=started enabled=true - name: Change postgres password for user postgres "{{database_pass}}" shell: | sudo -i -u postgres psql -c "ALTER USER postgres PASSWORD '{{database_pass}}';" - name: delete peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/9.4/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: absent - name: add parameters for work password postgres blockinfile: dest: /var/lib/pgsql/9.4/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all md5 host all all 0.0.0.0/0 md5 state: present - name: set postgresql.conf access lineinfile: dest: /var/lib/pgsql/9.4/data/postgresql.conf line: "listen_addresses = '*'" state: present - name: restart postgres service: name=postgresql-9.4 state=restarted |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/postgresql-9.5.yml
[codesyntax lang="php"]
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 |
- name: Install REPO postgresql-9.5 yum: name: https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm state: present - name: Install postgresql-9.5 yum: name: "{{ packages }}" state: present vars: packages: - postgresql95 - postgresql95-server - postgresql95-contrib - postgresql95-libs - name: initialize postgresql command: /usr/pgsql-9.5/bin/postgresql95-setup initdb - name: Add pg_hba for postgresql-9.5 template: src=/etc/ansible/roles/databases/templates/pg_hba.conf dest=/var/lib/pgsql/9.5/data/pg_hba.conf - name: add peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/9.5/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: present - name: Start the postgresql-9.5 service service: name=postgresql-9.5 state=started enabled=true - name: Change postgres password for user postgres "{{database_pass}}" shell: | sudo -i -u postgres psql -c "ALTER USER postgres PASSWORD '{{database_pass}}';" - name: delete peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/9.5/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: absent - name: add parameters for work password postgres blockinfile: dest: /var/lib/pgsql/9.5/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all md5 host all all 0.0.0.0/0 md5 state: present - name: set postgresql.conf access lineinfile: dest: /var/lib/pgsql/9.5/data/postgresql.conf line: "listen_addresses = '*'" state: present - name: restart postgres service: name=postgresql-9.5 state=restarted |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/postgresql-9.6.yml
[codesyntax lang="php"]
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 |
- name: Install REPO postgresql-9.6 yum: name: https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm state: present - name: Install postgresql-9.6 yum: name: "{{ packages }}" state: present vars: packages: - postgresql96 - postgresql96-server - postgresql96-contrib - postgresql96-libs - name: initialize postgresql command: /usr/pgsql-9.6/bin/postgresql96-setup initdb - name: Add pg_hba for postgresql-9.6 template: src=/etc/ansible/roles/databases/templates/pg_hba.conf dest=/var/lib/pgsql/9.6/data/pg_hba.conf - name: add peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/9.6/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: present - name: Start the postgresql-9.6 service service: name=postgresql-9.6 state=started enabled=true - name: Change postgres password for user postgres "{{database_pass}}" shell: | sudo -i -u postgres psql -c "ALTER USER postgres PASSWORD '{{database_pass}}';" - name: delete peer/trust access, for change password blockinfile: dest: /var/lib/pgsql/9.6/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all peer host all all all trust state: absent - name: add parameters for work password postgres blockinfile: dest: /var/lib/pgsql/9.6/data/pg_hba.conf marker: "#{mark} ANSIBLE MANAGED BLOCK for add parameters to change password postgres" block: | local all all md5 host all all 0.0.0.0/0 md5 state: present - name: set postgresql.conf access lineinfile: dest: /var/lib/pgsql/9.6/data/postgresql.conf line: "listen_addresses = '*'" state: present - name: restart postgres service: name=postgresql-9.6 state=restarted |
[/codesyntax]
[root@ansible ~]# cat /etc/ansible/roles/databases/tasks/repo.yml
[codesyntax lang="php"]
1 2 3 4 5 6 7 8 9 10 |
- name: Import EPEL GPG key. rpm_key: key: /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-{{ ansible_distribution_major_version }} state: present - name: Install EPEL repo. yum: name: https://dl.fedoraproject.org/pub/epel/epel-release-latest-{{ ansible_distribution_major_version }}.noarch.rpm state: present |
[/codesyntax]