一、认识ansible
Ansible是一款开源自动化运维工具。它有如下特点:
1、不需要安装客户端,通过sshd去通信,比较轻量化;
2、基于模块工作,模块可以由任何语言开发,比较自由和开放;
3、不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读;
4、安装十分简单,RHEL/Rocky上可直接yum安装;
5、有提供UI(浏览器图形化)http://www.ansible.com/tower,但是收费的;
6、目前Ansible已经被RedHat公司收购,它在Github上是一个非常受欢迎的开源软件
二、安装ansible
1.机器准备(用的redhat 9)
主机名 | 作用 |
---|---|
control(192.168.85.133) | 控制端 |
manged (192.168.85.129) | 被控制端 |
2.控制端安装ansible
bash
dnf install ansible-core 自带的版本较低
bash
使用pipx进行网络安装
python3 -m pip install --user pipx
【root@192 】pip install ansible
3.主机改名
bash
[root@localhost ~]# hostnamectl set-hostname control
[root@localhost ~]# hostnamectl set-hostname manged
4.设置密钥认证,ssh无密码登录
ssh-keygen生成一对密钥(公钥和密钥);ssh-copy-id把本地主机的公钥复制到目标主机上,会在/root/。ssh下生成,id_rsa为私钥,rsa.pub为公钥,它会被发送到远程主机的.ssh目录。
bash
[root@control ~]# ssh-keygen
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):
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:ix4x2B2W0UYMEo3mXTRdjBk80akGaw4HcR95hpnvKGk root@control
The key's randomart image is:
+---[RSA 3072]----+
| o++B=o+&.. |
| o.o+*o@.* |
| o .++ o.* |
| o.oo.+ o . |
| . + S= o o |
| + .E . . |
| o .. . |
| . . |
| . |
+----[SHA256]-----+
bash
[root@control ~]# ssh-copy-id 192.168.85.129
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.85.129 (192.168.85.129)' can't be established.
ED25519 key fingerprint is SHA256:pZV20K/MGiBcrjDwuiY8PCjvlRuVtMNZ2xsE4ahgJbg.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.85.129'"
and check to make sure that only the key(s) you wanted were added.
ssh远程连接就不需要密码了。
4.通过tree查看ansible的主要文件
bash
root@localhost ~] tree /etc/ansible/
/etc/ansible/
├── ansible.cfg #主配置文件
├── hosts #主机清单文件 必须写
└── roles #公共角色文件
bash
[root@localhost ~] vim /etc/ansible/hosts
[liang] #组中两个被管理机的IP以及组名(相当于组长),组名名字可以随便取
192.168.16.135
192.168.16.136
没有组的主机要在有组的前面
查看主机清单的主机结构
bash
[root@control ansible]# ansible-inventory --graph
@all:
|--@ungrouped:
|--@webserver:
| |--manged
如果写的域名还需在/etc/hosts写配置
5.测试
bash
[root@localhost ~] ansible all -m ping
#解释:用于测试远程主机是否在线,回复pong表示在线
#ping模块用于检查指定节点机器(被管理主机)是否还能连通,
#用法简单,不涉及参数,主机如果在线,则回复pong
二、ansible常用模块
root@control ansible\]# ansible-doc yum 查看模块用法 \[root@localhost \~\]# ansible-doc -l 查看ansibe中已有的模块 ansible调用格式: ansible \[节点\] -m \[模块\] -a \[参数
1.setup模块
root@localhost \~\]# ansible all -m setup 查看某个节点的信息 **2.copy模块** 该模块可实现从管理机向节点复制静态文件,并设置合理的权限 ```bash 参数 选项 含义 dest --- 文件复制的目的地 src --- 复制的源文件 backup yes/no 是否备份原始文件 validate - 复制前是否检验需要复制目的路径 mode --- 给文件加权限 ``` 使用:ansible all -m copy -a 'src= \~ /xixi.txt dest=\~/ mode=777' **3.command模块和shell模块** 默认是command模块,command模块是不支持管道,》,《等使用的,但是shell脚本可以。 使用:ansible all -m shell -a "ls -l /" ansible all -m shell -a "ps -ef \| httpd" **4.script模块** cript模块用于将管理机的shell脚本发送到节点上执行。在实际的工作中,经常会发送脚本控制远程主机执行指定任务,所有该模块使用会频繁使用。 ```bash 写一个脚本 [root@control ~]# vim xi.sh [root@control ~]# cat xi.sh !#/bin/bash echo "hello ansible" `touch hell.txt` [root@control ~]# ansible all -m script -a "~/xi.sh" 在目标主机执行后出现该文件 ``` **5、yum模块** yum模块能够从指定的服务器自动下载安装RPM包,并且可以自动处理依赖性关系。 ```bash 参数 选项 含义 name --- 包名 state present 安装(默认) latest 更新 absent 卸载 ``` 使用: ansible all -m yum -a "name=vsftpd state=present" **6.serice模块** service模块用来管理节点上的服务,可开启,关闭,重启服务,如httpd,sshd,nfs,named等服务。 ```bash 参数 选项 含义 enabled yes/no 是否开机自启动 name --- 服务名称 pattern --- 若服务没响应,则ps查看是否已经启动 start 启动 state stoped 关闭 restarted 重启 reloaded 重新下载 ``` 使用:ansible all -m shell -a "systemctl stop httpd" ### 三、Playbook 命令是在节点上执行任务的,使用起来复杂,且重复,为避免重复,ansible提供playbook脚本,一个可以被ansible执行的YAML文件叫做playbook。 **Playbook示例 安装httpd服务** ```bash 示例:httpd.yml - hosts: websrvs remote_user: root tasks: - name: Install httpd yum: name=httpd state=present - name: Install configure file copy: src=files/httpd.conf dest=/etc/httpd/conf/ - name: start service service: name=httpd state=started enabled=yes ```