ansible 安装和测试

一、环境准备
[root@master ~]# cat /etc/redhat-release

CentOS release 6.5 (Final)

       hostname       ip
Master    master         10.0.0.28
Minion    client01        10.0.0.20
Minion    client02        10.0.0.21
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

二、安装Ansible

  1. 更改yum 源:

[root@master ~]#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

2.软件包安装
EPEL 已经提供了ansible 所需的所有支持软件包,所以在这里使用epel 源进行安装:

[root@master ~]#
rpm -ivh  http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
Retrieving http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.MYaX9K: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           warning: /etc/yum.repos.d/epel.repo created as /etc/yum.repos.d/epel.repo.rpmnew
########################################### [100%]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

3.安装Ansible

[root@master ~]# yum install ansible -y
4.免密要认证

[root@master ~]#ssh-keygen -t rsa
 ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.20
 ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.21
 cp /etc/ansible/hosts /etc/ansible/hosts.ori
 > /etc/ansible/hosts
 vim /etc/ansible/hosts
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

ansible 默认提供了很多模块来供我们使用。在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc -s 模块名 又可以查看该模块有哪些参数可以使用。

5.建立host文件
Ansible 的host文件默认在/etc/ansible/ 这个目录下面,采用rpm安装的ansible会将该host文件作为范例,其中提示ansible是支持域名和IP的两种客户端命名格式的,进过测试时没有问题的,还支持不同的安装分组方法,建议好好看看,这里提供三台机器,分为master client01 client02 将他们分为两组 master 和slave

[root@master ~]# cat /etc/ansible/hosts
[localhost]
127.0.0.1
[slave]
10.0.0.20
10.0.0.21
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

6.测试ansible 的使用

在这里使用ping模块:

[root@master ~]# ansible slave -i /etc/ansible/hosts -m ping
10.0.0.20 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.0.0.21 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

解读:从返回值分析,ansiable salve 接待你10.0.0.20,10.0.0.21 的ping
值成功。说明ansiable 已经能够使用。

7.验证是否支持域名解析测试:

tail -3 /etc/hosts
10.0.0.28 master.test.com
10.0.0.21 client02
10.0.0.20 agent.test.com
  • 1.
  • 2.
  • 3.
  • 4.
[root@master ~]# cat /etc/ansible/hosts
[localhost]
master.test.com
[slave]
client02
agent.test.com
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

[root@master ~]# ansible slave -i /etc/ansible/hosts -m ping

paramiko: The authenticity of host 'agent.test.com' can't be established.
The ssh-rsa key fingerprint is 3d906ef1d450e4cc7031aef5e8c296f6.
Are you sure you want to continue connecting (yes/no)?
yes

paramiko: The authenticity of host 'client02' can't be established.
The ssh-rsa key fingerprint is 3d906ef1d450e4cc7031aef5e8c296f6.
Are you sure you want to continue connecting (yes/no)?
agent.test.com | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
yes
client02 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@master ~]# ansible slave -i /etc/ansible/hosts -m ping
agent.test.com | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
client02 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
  • 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.

验证是支持服务器本地域名解析的

posted @   勤奋的蓝猫  阅读(9)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
点击右上角即可分享
微信分享提示