【Redhat-自动化运维工具ansible的安装和配置】

ansible是一款自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。安装Ansible之后,不需要启动或运行一个后台进程,或是添加一个数据库。只要在一台电脑(可以是一台笔记本)上安装好,就可以通过这台电脑管理一组远程的机器。在远程被管理的机器上,不需要安装运行任何软件,因此升级Ansible版本不会有太多问题。目前,只要机器上安装了 Python 2.6 或 Python 2.7 (windows系统不可以做控制主机),都可以运行Ansible。

一 .配置本地yum仓库和在线仓库

在/etc/yum.repos.d目录下创建red8.repo仓库文件。

[root@Redhat-node-1 ~]# cd /etc/yum.repos.d/
[root@Redhat-node-1 yum.repos.d]# vim red8.repo

在/etc/yum.repos.d/red8.repo中写入以下内容

  1 [a]                     #yum仓库名
  2 name=a 
  3 baseurl=file:///mnt/zhang/AppStream    #本地yum仓库源地址:是你挂载的地址
  4 gpgcheck=0                   #是否gpg校验
  5 enable=1                    #是否可用
  6 
  7 [b]
  8 name=b
  9 baseurl=file:///mnt/zhang/BaseOS
 10 gpgcheck=0
 11 enable=1
 12 
 13 
 14 
 15 [c]              #在线yum源:这儿选用清华大学官方的y在线um源
 16 name=c
 17 baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/8/Everything/x86_64/
 18 gpgcheck=0
 19 enable=1

接着使用yum clean all 清除缓存

[root@Redhat-node-1 yum.repos.d]# yum clean all

使用yum makecache重新生成缓存

[root@Redhat-node-1 yum.repos.d]# yum makecache

输出结果如下说明成功了

 二.下载ansible并配置

[root@Redhat-node-1 yum.repos.d]# yum -y install ansible

下载ansible后需注意他的几个文件

 常用的是:ansible.cfg和hosts文件系统默认的这两个文件位于:/etc/ansible 下。一般我们是自己创建这两个文件,方法如下:

[root@Redhat-node-1 ~]# mkdir /root/ansible
[root@Redhat-node-1 ~]# cd /root/ansible/
[root@Redhat-node-1 ansible]# vim ansible.cfg

写入:

 在/root/ansible目录下创建主机清单文件并写入主机

[root@Redhat-node-1 ansible]# vim hosts 
  1 [total]               #组名
  2 192.168.175.132    #主机IP地址
  3 192.168.175.133
  4 192.168.175.134
  5 
  6 [server]          #组名
  7 Redhat-node-3     #主机名:写了主机名需要在/etc/hosts文件中将主机的IP地址和主机名写入。
  8 Redhat-node-4
  9 
 10 [db]
 11 Redhat-node-2

在工作目录中执行ansible命令的ping模块

[root@Redhat-node-1 ansible]# ansible total -m ping 

如报错

 这是因为ansible是ssh密钥认证的,解决方法:

[root@Redhat-node-1 ansible]# 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:BvJMV+VYOM4Bt1ZDg+G0beAGLpwMBpVj//uXl32kJdQ root@Redhat-node-1
The key's randomart image is:
+---[RSA 3072]----+
|  .o+.  o.**B    |
|   .++ o B=X o   |
|   ..oB ooOo+  . |
|     =.+ oo.  . E|
|      o.S    .   |
|       ..     . o|
|         .   . B |
|        .   o + o|
|         ... .  .|
+----[SHA256]-----+

#一路回车
[root@Redhat-node-1 ansible]# ssh-copy-id 192.168.175.132      #将公钥发送到被管理机中
/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.175.132's password:       #输入被管理机的root密码

Number of key(s) added: 1

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



#其他的被管理机同样执行此操作

进入到工作目录中再次测试

[root@Redhat-node-1 ansible]# ansible total -m ping 
192.168.175.132 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.175.134 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.175.133 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

即可成功

三.总结

ansible采用的是ssh的方式,其配置文件的详细注释如下

#ansible主配置文件:/etc/ansible/ansible.cfg,一般保持默认,修改配置文件后不需要重启。
[defaults]
# some basic default values...
#inventory = /etc/ansible/hosts   #主机列表配置文件
#library = /usr/share/my_modules/  #库文件存放目录
#remote_tmp = ~/.ansible/tmp  #临时py命令文件存放在远程主机目录
#local_tmp = ~/.ansible/tmp  #本机的临时命令执行目录
#forks = 5  #默认并发数,可理解为每批次连接5台主机操作。
#sudo_user = root  #默认sudo用户
#ask_sudo_pass = True  #每次执行ansible命令是否询问ssh密码
#ask_pass  = True
#remote_port = 22
#host_key_checking = False  #检查对应服务器的host_key,建议取消注释,不检查,										提高ssh连接便捷性。
#log_path = /var/log/ansible.log  #日志文件,建议取消注释,启用记录日志。

非常适合做服务器集群管理,真的适合学习运维的朋友。下次给大家分享ansible 部署httpd实现在线yum源。

                  给个关注不迷路,谢谢啦!

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值