在 CentOS 7 中安装并使用自动化工具 Ansible
Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具。它用Python写成,类似于Chef和Puppet,但是有一个不同和优点是我们不需要在节点中安装任何客户端。它使用SSH来和节点进行通信。
本篇中我们将在CentOS 7上安装并配置Ansible,并且尝试管理两个节点。
- Ansible 服务端 – ansible.linuxtechi.com ( 192.168.1.15 )
-
节点 – 192.168.1.9 , 192.168.1.10
第一步: 设置EPEL仓库
Ansible仓库默认不在yum仓库中,因此我们需要使用下面的命令启用epel仓库。
[root@ansible ~]# rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
第二步: 使用yum安装Ansible
[root@ansible ~]# yum install ansible
安装完成后,检查ansible版本:
[root@ansible ~]# ansible --version
第三步: 设置用于节点鉴权的SSH密钥
在Ansible服务端生成密钥,并且复制公钥到节点中。
root@ansible ~]# ssh-keygen
使用ssh-copy-id命令来复制Ansible公钥到节点中。
第四步:为Ansible定义节点的清单
文件 /etc/ansible/hosts
维护着Ansible中服务器的清单。
[root@ansible ~]# vi /etc/ansible/hosts [test-servers] 192.168.1.9 192.168.1.10
保存并退出文件。
主机文件示例如下:
第五步:尝试在Ansible服务端运行命令
使用ping检查‘test-servers’或者ansible节点的连通性。
[root@ansible ~]# ansible -m ping 'test-servers'
执行shell命令
例子1:检查Ansible节点的运行时间(uptime)
[root@ansible ~]# ansible -m command -a "uptime" 'test-servers'
例子2:检查节点的内核版本
[root@ansible ~]# ansible -m command -a "uname -r" 'test-servers'
例子3:给节点增加用户
[root@ansible ~]# ansible -m command -a "useradd mark" 'test-servers' [root@ansible ~]# ansible -m command -a "grep mark /etc/passwd" 'test-servers'
例子4:重定向输出到文件中
[root@ansible ~]# ansible -m command -a "df -Th" 'test-servers' > /tmp/command-output.txt
via: http://www.linuxtechi.com/install-and-use-ansible-in-centos-7/
作者:Pradeep Kumar 译者:geekpi 校对:wxy
原文地址:https://linux.cn/article-6347-1.html
相关推荐
-
IPv6 互联网中的隐私保护和网络管理器 服务器
2019-3-10
-
多交换机间的VLAM通信 服务器
2019-9-10
-
用 screenfetch 和 linux_logo 显示带有酷炫 Linux 标志的基本硬件信息 服务器
2019-3-10
-
TODO:MongoDB MySQL数据库备份 服务器
2019-10-16
-
把.Net开发环境迁移到Linux上去 服务器
2019-3-20
-
mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等) 服务器
2019-2-16
-
MySQL 并行复制(MTS) 从库发生异常crash分析 服务器
2019-7-23
-
关于MySQL优化的几点总结 服务器
2019-3-17
-
你可能不知道的CSS 计数器 服务器
2020-6-28
-
MySQL对JSON类型UTF-8编码导致中文乱码探讨 服务器
2020-7-4