加入收藏 | 设为首页 | 会员中心 | 我要投稿 莱芜站长网 (https://www.0634zz.com/)- 云连接、建站、智能边缘云、设备管理、大数据!
当前位置: 首页 > 综合聚焦 > CentOS > 正文

CentOS 7 中安装并使用自动化工具 Ansible

发布时间:2020-09-19 10:18:28 所属栏目:CentOS 来源:互联网
导读:我们将在CentOS 7上安装并配置Ansible,并且尝试管理两个节点。 Ansible 服务端C ansible.linuxtechi.com ( 192.168.1.15 ) 节点C 192.168.1.9 , 192.168.1.10 第一步: 设置EPEL仓库 Ansible仓库默认不在yum仓库中,因此我们需要使用下面的命令启用epel仓库

我们将在CentOS 7上安装并配置Ansible,并且尝试管理两个节点。

  • Ansible 服务端C ansible.linuxtechi.com ( 192.168.1.15 )

  • 节点C 192.168.1.9,192.168.1.10

第一步: 设置EPEL仓库

Ansible仓库默认不在yum仓库中,因此我们需要使用下面的命令启用epel仓库。

[root@ansible~]#rpm-iUvhhttp://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

第二步: 使用yum安装Ansible

[root@ansible~]#yuminstallansible

安装完成后,检查ansible版本:

[root@ansible~]#ansible--version

CentOS 7 中安装并使用自动化工具 Ansible

ansible-version

第三步: 设置用于节点鉴权的SSH密钥

在Ansible服务端生成密钥,并且复制公钥到节点中。

root@ansible~]#ssh-keygen

CentOS 7 中安装并使用自动化工具 Ansible

ssh-keygen

使用ssh-copy-id命令来复制Ansible公钥到节点中。

CentOS 7 中安装并使用自动化工具 Ansible

ssh-copy-id-command

第四步:为Ansible定义节点的清单

文件/etc/ansible/hosts维护着Ansible中服务器的清单。

[root@ansible~]#vi/etc/ansible/hosts[test-servers]192.168.1.9192.168.1.10

保存并退出文件。

主机文件示例如下:

CentOS 7 中安装并使用自动化工具 Ansible

ansible-host

第五步:尝试在Ansible服务端运行命令

使用ping检查‘test-servers’或者ansible节点的连通性。

[root@ansible~]#ansible-mping'test-servers'

CentOS 7 中安装并使用自动化工具 Ansible

ansible-ping

执行shell命令

例子1:检查Ansible节点的运行时间(uptime)

[root@ansible~]#ansible-mcommand-a"uptime"'test-servers'

CentOS 7 中安装并使用自动化工具 Ansible

ansible-uptime

例子2:检查节点的内核版本

[root@ansible~]#ansible-mcommand-a"uname-r"'test-servers'

CentOS 7 中安装并使用自动化工具 Ansible

kernel-version-ansible

例子3:给节点增加用户

[root@ansible~]#ansible-mcommand-a"useraddmark"'test-servers'[root@ansible~]#ansible-mcommand-a"grepmark/etc/passwd"'test-servers'

CentOS 7 中安装并使用自动化工具 Ansible

useradd-ansible

例子4:重定向输出到文件中

[root@ansible~]#ansible-mcommand-a"df-Th"'test-servers'>/tmp/command-output.txt

CentOS 7 中安装并使用自动化工具 Ansible

(编辑:莱芜站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读