ansible批量安装postgresql软件

ansible批量安装postgresql软件

随着分布式系统和大规模应用的普及,自动化部署和管理变得越来越重要。Ansible 是一种流行的自动化工具,它可以帮助我们简化和加速服务器的配置和管理任务。本文将详细介绍如何使用 Ansible 来批量安装 PostgreSQL 数据库,以实现高效的部署和维护。

一、准备工作

1.1. 环境准备

•操作系统:Red Hat Enterprise Linux(RHEL)或其衍生版本(如 CentOS)。

•软件版本:本文以 PostgreSQL 16 为例,适用于大多数主流 Red Hat 基础设施。

•Ansible 版本:确保你的 Ansible 版本至少为 2.8 或更高版本。

安装规划表

目标服务器

IP地址

pg版本

kylin v10

192.168.2.136

v16

kylin v10

192.168.2.137

v16

kylin v10

192.168.2.138

v16

1.2. 安装 Ansible

如果你还没有安装 Ansible,可以通过以下命令在控制节点(即运行 Ansible 命令的机器)上安装:

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

sudo yum install epel-release

sudo yum install ansible

1.3. 配置 SSH 互信

为了能够在不输入密码的情况下执行远程命令,需要配置 SSH 密钥对。在控制节点上生成 SSH 密钥对,并将公钥添加到目标服务器的 ~/.ssh/authorized_keys 文件中。

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

1、生成密钥

 ssh-keygen 

2、查看公钥

 cat ~/.ssh/id_rsa.pub 

3、查看私钥

cat ~/.ssh/id_rsa

4、传输公钥到目标服务器

ssh-copy-id 192.168.77.100

5、清空间密钥 

echo '' > ~/.ssh/id_rsa

echo '' >  ~/.ssh/id_rsa.pub

二、创建 Ansible Playbook

2.1. 创建文件夹结构在控制节点上创建一个工作目录,并在其中设置基本的文件结构:

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

[root@db1 roles]# tree  postgresql/

postgresql/

├── files

│   ├── pg_install.sh

│   └── postgresql-16.4.tar.gz

├── handlers

├── tasks

│   ├── create_user.yml

│   ├── depent_package_install.yml

│   ├── main.yml

│   └── pg_install.yml

└── templates

2.2. 编写 hosts 文件在 hosts 文件中定义目标服务器。

例如:

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

[root@db1 ansible]# cat /etc/ansible/hosts 

[dbservers]

#192.168.2.136 ansible_python_interpreter=/usr/bin/python3

#192.168.2.137 ansible_python_interpreter=/usr/bin/python3

#192.168.2.138 ansible_python_interpreter=/usr/bin/python3

192.168.2.136

192.168.2.137

192.168.2.138

[dbservers:vars]

ansible_python_interpreter=/usr/bin/python3

2.3. 编写变量文件在 group_vars/all.yml 文件中定义全局变量:

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

---

postgres_version: "16"

postgres_package: "postgresql{{ postgres_version }}"

2.4. 编写任务文件main.yml

在 tasks/main.yml 文件中编写具体的安装任务:

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

[root@db1 tasks]# cat main.yml 

- include_tasks: create_user.yml

- include_tasks: depent_package_install.yml

- include_tasks: pg_install.yml

2.5.编写任务文件create_user.yml

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

# cat create_user.yml 

- name: create pg group

  group:

    name: postgresql  

    gid: 2000

- name: create pg user

  user:

    name: postgresql

    uid: 2000 

    group: postgresql

    state: present

- name: check user

  command: id postgresql

  register: show_user

- debug: var=show_user.stdout

2.6.编写任务文件depent_package_install.yml

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

# cat depent_package_install.yml 

- name: install pg dependent packages

  yum:

  name: "{{ item }}"

  state: present

  loop:

    - bison 

    - flex 

    - readline-devel

    - zlib-devel

    - gcc  

    - libxml2  

    - libxml2-devel 

    - lz4 

    - lz4-devel 

    - systemtap-sdt-devel 

    - perl-ExtUtils-Embed  

    - perl-ExtUtils-MakeMaker

    - openssl 

    - openssl-devel

    - pam 

    - pam-devel

    - libxslt 

    - libxslt-devel

    - systemd-devel

    - tcl 

    - tcl-devel

    - libicu-devel

2.7.编写任务文件pg_install.yml

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

# cat pg_install.yml 

- name: copy db software to target hosts

  copy: src=postgresql-16.4.tar.gz dest=/tmp

- name: copy postgresql install scripts

  copy: src=pg_install.sh dest=/tmp/pg_install.sh owner=root group=root mode=755

- name: install postgresql software

  shell: /bin/bash /tmp/pg_install.sh

2.8. 创建模板文件在工作目录下创建 templates 文件夹,并在其中放置两个模板文件:

  1. postgresql.conf.j2
  1. pg_hba.conf.j2

示例内容如下:

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

# templates/postgresql.conf.j2

listen_addresses = '*'

max_connections = 100

shared_buffers = 128MB

maintenance_work_mem = 64MB

autovacuum_max_workers = 32

wal_level = replica

plaintext# templates/pg_hba.conf.j2

# TYPE DATABASE USER ADDRESS CIDR-ADDRESS METHOD

local all postgres peer

local all all md5

host all all 127.0.0.1/32 md5

host all all ::1/128 md5

三、运行 Ansible Playbook

在控制节点上运行以下命令来启动安装过程:

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

ansible-playbook -i hosts tasks/main.yml

四、验证安装

安装完成后,可以登录到目标服务器上验证 PostgreSQL 是否正确安装并运行:

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

ssh user@192.168.1.10

systemctl status postgresql

如果一切顺利,你应该能看到 PostgreSQL 服务正在运行,并且可以通过 ps aux | grep postgres 查看相关进程。

五、清除环境

Plain TextJavaScriptPythonJavaGoRubyTypeScriptHTMLCSSC/C++PHPC#ScalaShellDartRustKotlinSwiftPowerShellGroovy-ABAPActionScriptAdaAppleScriptArduinoASP.NET (C#)AtomAutoHotkeyAutoItBashBASICBatchClojureCoffeeScriptDDiffDockerEditorConfigElixirElmERBErlangF#FortranGraphQLHaskellHaxeJSONJuliaWeb App ManifestLaTeXTeX ConTeXtLessLispLiveScriptLuaMakefileMarkdownMarkup templatingMathematicaMathMLMATLABNASMnginxObjective-COCamlObject PascalPascalPerlPL/SQLPrologPuppetPureScriptQMLRRacketReact JSXReact TSXReasonreStructuredTextRSSSass (Sass)Sass (Scss)SmalltalkSplunk SPLSQLSSMLSVGTclTextileTypoScriptValaVB.NetVerilogVHDLvimVBAVisual BasicWiki markupYAMLXQueryXML

[root@db1 roles]# rm -rf /tmp/postgresql-16.4*

[root@db1 roles]#  rm -rf /usr/local/postgres/

[root@db1 roles]# userdel postgresql

六、总结

通过使用 Ansible,我们可以轻松地批量安装和配置 PostgreSQL 数据库,从而大大提高了部署效率和一致性。本文提供了一个基本的框架,可以根据具体需求进行扩展和定制。希望这篇教程对你有所帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值