前言:

1、Ansible可以同时管理Redhat系的Linux,Debian系的Linux,以及Windows主机。管理节点只在执行脚本时与远程主机连接,没有特别的同步机制,所以断电等异常一般不会影响ansbile。

2、ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

3、ansible的架构:连接其他主机默认使用ssh协议

管理端:192.168.200.20
被管理端1:192.168.200.30
被管理端2:192.168.200.40

1、关闭防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0

2、安装相关软件

[root@localhost ~]# yum -y install epel-release.noarch 
[root@localhost ~]# yum install -y ansible
[root@localhost ~]# yum install -y tree


[root@localhost ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg   #ansible配置文件
├── hosts         #管理主机的文件,主机清单
└── roles

3、修改主机清单

[root@localhost ~]# vim /etc/ansible/hosts    #配置主机清单
#以下内容为添加内容,不是修改内容
[webservers]
192.168.200.30    #被监控端1的IP
[mysql]
192.168.200.40    #被监控端2的IP

4、创建密钥对,进行远程连接

#生成密钥对
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   #回车
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):      #输入密码abc123
Enter same passphrase again:                     #确认密码abc123
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:WKpIMqbJgc2JyTz2C351d7bzbDJ83wUsyf1oKXaq82c root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                 |
|        .        |
|+= .   +   . +   |
|*B=   o S   + +  |
|==+. o . . o . = |
|oo..o . . + + = o|
|. ...     .B.BE o|
| ...      .+X=...|
+----[SHA256]-----+


[root@localhost ~]# ls -al
drwx------.  2 root root   38 4   2 08:54 .ssh

[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub    #id_rsa:私钥;id_rsa.pub:公钥

5、将公钥上传到被监控端

[root@localhost .ssh]# ssh-copy-id root@192.168.200.30

[root@localhost .ssh]# ssh-copy-id root@192.168.200.40


#在监控端登录被监控端查看日期
[root@localhost ~]# ansible 192.168.200.30 -m command -a 'date'
Enter passphrase for key '/root/.ssh/id_rsa':    #输入之前设置的密码:abc123
192.168.200.30 | CHANGED | rc=0 >>
2021 04 02 星期五 11:23:16 CST


[root@localhost ~]# ansible mysql -m command -a 'date'   #当然也可以用别名进程查看
Enter passphrase for key '/root/.ssh/id_rsa': 
192.168.200.40 | CHANGED | rc=0 >>
2021 04 02 星期五 11:24:20 CST


-m:指定模块
-a:指定参数

6、设置ssh免交互登录

[root@localhost ~]# ssh-agent bash
[root@localhost ~]# ssh-add
Enter passphrase for /root/.ssh/id_rsa: 
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐