发表于 2018-7-29 06:46:28

ansible 安装和测试

  一、环境准备
  # cat /etc/redhat-release
  

CentOS>
  hostname       ip
  
Master    master         10.0.0.28
  
Minion    client01      10.0.0.20
  
Minion    client02      10.0.0.21
  

  二、安装Ansible


[*]更改yum 源:
  #wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
  2.软件包安装
  EPEL 已经提供了ansible 所需的所有支持软件包,所以在这里使用epel 源进行安装:
  

#  
rpm -ivhhttp://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>  
Preparing...                ###########################################
  1:epel-release         warning: /etc/yum.repos.d/epel.repo created as /etc/yum.repos.d/epel.repo.rpmnew
  
###########################################
  

  3.安装Ansible
  # yum install ansible -y
  4.免密要认证
  

#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
  

  ansible 默认提供了很多模块来供我们使用。在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc-s模块名又可以查看该模块有哪些参数可以使用。
  5.建立host文件
  Ansible 的host文件默认在/etc/ansible/ 这个目录下面,采用rpm安装的ansible会将该host文件作为范例,其中提示ansible是支持域名和IP的两种客户端命名格式的,进过测试时没有问题的,还支持不同的安装分组方法,建议好好看看,这里提供三台机器,分为master client01client02将他们分为两组 master 和slave
  

# cat /etc/ansible/hosts  

  
127.0.0.1
  

  
10.0.0.20
  
10.0.0.21
  

  6.测试ansible 的使用
  在这里使用ping模块:
  

# 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"
  
}
  
}
  

  解读:从返回值分析,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
  

# cat /etc/ansible/hosts  

  
master.test.com
  

  
client02
  
agent.test.com
  

  # 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"
  
}
  
# ansible slave -i /etc/ansible/hosts -m ping
  
agent.test.com | SUCCESS => {
  "changed": false,
  "ping": "pong"
  
}
  
client02 | SUCCESS => {
  "changed": false,
  "ping": "pong"
  
}
  

  验证是支持服务器本地域名解析的
页: [1]
查看完整版本: ansible 安装和测试