目录

1.配置ssh密钥和上传

 2.安装ansible

3.配置

4.远程安装

5.卸载


简介:

Ansible是一种自动化工具,用于自动化IT环境中的配置管理、应用程序部署、任务协调和远程执行等任务。它是一种基于Python的开源工具,可以轻松地管理大规模的复杂IT环境,可以使用Ansible来自动化配置和管理负载均衡器、集群、故障转移等功能,以提高系统的可用性和容错性。

核心组件:

1. Ansible Core:Ansible的核心组件,包括Ansible的命令行工具和核心库。

2. Ansible Playbooks:Ansible的主要配置文件,用于定义任务和执行步骤。

3. Ansible Modules:Ansible的模块,用于执行各种任务,如文件操作、软件包管理、用户管理等。

4. Ansible Inventory:Ansible的清单,用于定义主机和组。

优点:

1. 简单易用:Ansible使用YAML语言编写,易于理解和编写。

2. 跨平台:Ansible可以在各种操作系统和云平台上运行。

3. 高效可靠:Ansible使用SSH协议进行通信,可以快速、可靠地执行任务。

4. 可扩展性:Ansible可以与其他工具集成,如Puppet、Chef等。

原理:

Ansible使用SSH协议进行通信,通过在远程主机上安装Ansible的代理程序,可以实现无需在远程主机上安装任何软件即可执行任务。Ansible使用Playbooks定义任务和执行步骤,使用Modules执行各种任务。

Ansible具有以下特点: 1. 简单易用:Ansible使用简单的YAML语法,易于理解和编写。

2. 无需客户端:Ansible不需要在远程主机上安装任何客户端软件,只需使用SSH协议即可。

3. 幂等性:Ansible任务是幂等的,即无论执行多少次,结果都是一致的。

4. 扩展性:Ansible可以通过编写自定义模块来扩展其功能,满足特定需求。

5. 社区支持:Ansible拥有庞大的社区支持,可以获取到丰富的文档和示例。

开始

操作环境

一台服务机 192.168.1.119

一台客户机 192.168.1.139    (可以多台)

一台托管数据库服务器  192.168.1.20

1.配置ssh密钥和上传

全程服务端

获取密钥

[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:nfqYMxcqpyXf28EcfdAeUAOihn3LDKR+cA3rTgjQTe8 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|   .. o. o . ooo |
|    .. .* = . ...|
|     . + O o  ...|
|      o B.=... o.|
|       oSEo+. . o|
|        +..o . . |
|      . oo .+    |
|      .+==.. .   |
|      .++++..    |
+----[SHA256]-----+

上传到客户端

[root@localhost ~]# ssh-copy-id 192.168.1.139
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.139's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.1.139'"
and check to make sure that only the key(s) you wanted were added.

 2.安装ansible

yum 安装 [root@localhost ~]# yum -y install ansible

3.配置

[root@localhost ~]# vim  /etc/ansible/ansible.cfg 
[defaults]
inventory = /etc/ansible/hosts
remote_user = root

这将设置Ansible使用 /etc/ansible/hosts 文件作为主机清单,并使用 root 用户进行远程连接。

 创建主机清单

主机清单是一个文本文件,其中包含要管理的所有主机的列表。可以使用以下命令创建一个名为hosts的主机清单文件

vim /etc/ansible/hosts

在文件中添加要管理的主机IP或主机名


[web]
192.168.1.139
[db]
192.168.1.120    
这将创建两个组:web和db,其中web组包含一个主机,db组包含一个主机。

这是一个简单的主机清单,其中包含了两个IP地址。其中,前一个IP地址(192.168.1.139)被标记为web,这可能意味着这个IP地址是用于托管Web服务器的主机。另外一个IP地址(192.168.1.120)被标记为db,这可能意味着这些IP地址是用于托管数据库服务器的主机。这些IP地址是在一个局域网中使用的,而不是在公共互联网上可访问的。

4.远程安装

现在可以使用Ansible进行yum安装。例如,要在web组中的主机上安装 nginx ,可以使用以下命令:

ansible web -m yum -a "name=nginx state=present"

这将使用 yum 模块在 web 组中的所有主机上安装 nginx 软件包。

[root@localhost ~]# ansible web -m yum -a "name=nginx state=present"
192.168.1.139 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "nginx"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.nju.edu.cn\n * epel: mirror-hnd.yuki.net.uk\n * extras: mirrors.nju.edu.cn\n * updates: mirrors.nju.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package nginx.x86_64 1:1.20.1-10.el7 will be installed\n--> Processing Dependency: nginx-filesystem = 1:1.20.1-10.el7 for package: 1:nginx-1.20.1-10.el7.x86_64\n--> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64\n--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64\n--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64\n--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-10.el7.x86_64\n--> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.20.1-10.el7.x86_64\n--> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64\n--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64\n--> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64\n--> Running transaction check\n---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed\n---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed\n---> Package nginx-filesystem.noarch 1:1.20.1-10.el7 will be installed\n---> Package openssl11-libs.x86_64 1:1.1.1k-5.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package                 Arch          Version                Repository   Size\n================================================================================\nInstalling:\n nginx                   x86_64        1:1.20.1-10.el7        epel        588 k\nInstalling for dependencies:\n centos-indexhtml        noarch        7-9.el7.centos         base         92 k\n gperftools-libs         x86_64        2.6.1-1.el7            base        272 k\n nginx-filesystem        noarch        1:1.20.1-10.el7        epel         24 k\n openssl11-libs          x86_64        1:1.1.1k-5.el7         epel        1.5 M\n\nTransaction Summary\n================================================================================\nInstall  1 Package (+4 Dependent packages)\n\nTotal download size: 2.4 M\nInstalled size: 6.7 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal                                              761 kB/s | 2.4 MB  00:03     \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : 1:openssl11-libs-1.1.1k-5.el7.x86_64                         1/5 \n  Installing : 1:nginx-filesystem-1.20.1-10.el7.noarch                      2/5 \n  Installing : centos-indexhtml-7-9.el7.centos.noarch                       3/5 \n  Installing : gperftools-libs-2.6.1-1.el7.x86_64                           4/5 \n  Installing : 1:nginx-1.20.1-10.el7.x86_64                                 5/5 \n  Verifying  : gperftools-libs-2.6.1-1.el7.x86_64                           1/5 \n  Verifying  : centos-indexhtml-7-9.el7.centos.noarch                       2/5 \n  Verifying  : 1:nginx-filesystem-1.20.1-10.el7.noarch                      3/5 \n  Verifying  : 1:nginx-1.20.1-10.el7.x86_64                                 4/5 \n  Verifying  : 1:openssl11-libs-1.1.1k-5.el7.x86_64                         5/5 \n\nInstalled:\n  nginx.x86_64 1:1.20.1-10.el7                                                  \n\nDependency Installed:\n  centos-indexhtml.noarch 0:7-9.el7.centos gperftools-libs.x86_64 0:2.6.1-1.el7\n  nginx-filesystem.noarch 1:1.20.1-10.el7  openssl11-libs.x86_64 1:1.1.1k-5.el7\n\nComplete!\n"
    ]
}

 这是一个Ansible任务的输出结果,其中包含了在IP地址为192.168.1.139的主机上安装nginx软件包的过程。输出结果包括了安装过程中的依赖关系和安装的软件包的版本、存储库和大小等信息。

 以及安装过程中的进度和验证结果。最后,输出显示已安装的软件包和其依赖项的列表。

5.卸载

 要卸载`nginx`软件包,可以使用以下命令

这将使用 yum 模块在 web 组中的所有主机上卸载 nginx 软件包。

[root@localhost ~]# ansible web -m yum -a "name=nginx state=absent"
192.168.1.139 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "removed": [
            "nginx"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "已加载插件:fastestmirror\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 nginx.x86_64.1.1.20.1-10.el7 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package        架构            版本                       源              大小\n================================================================================\n正在删除:\n nginx          x86_64          1:1.20.1-10.el7            @epel          1.7 M\n\n事务概要\n================================================================================\n移除  1 软件包\n\n安装大小:1.7 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  正在删除    : 1:nginx-1.20.1-10.el7.x86_64                                1/1 \n  验证中      : 1:nginx-1.20.1-10.el7.x86_64                                1/1 \n\n删除:\n  nginx.x86_64 1:1.20.1-10.el7                                                  \n\n完毕!\n"
    ]
}

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐