建设银行网站入口,深圳seo优化信,深圳做网站龙华新科,网页空间一、认识ansible
Ansible是一款开源自动化运维工具。它有如下特点#xff1a;
1、不需要安装客户端#xff0c;通过sshd去通信#xff0c;比较轻量化#xff1b; 2、基于模块工作#xff0c;模块可以由任何语言开发#xff0c;比较自由和开放#xff1b; 3、不仅支持命…一、认识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
主机名作用control192.168.85.133控制端manged 192.168.85.129被控制端
2.控制端安装ansible
dnf install ansible-core 自带的版本较低使用pipx进行网络安装
python3 -m pip install --user pipx
【root192 】pip install ansible3.主机改名
[rootlocalhost ~]# hostnamectl set-hostname control
[rootlocalhost ~]# hostnamectl set-hostname manged4.设置密钥认证ssh无密码登录 ssh-keygen生成一对密钥公钥和密钥ssh-copy-id把本地主机的公钥复制到目标主机上会在/root/。ssh下生成id_rsa为私钥rsa.pub为公钥它会被发送到远程主机的.ssh目录。
[rootcontrol ~]# 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 rootcontrol
The keys randomart image is:
---[RSA 3072]----
| oBo.. |
| o.o*o.* |
| o . o.* |
| o.oo. o . |
| . S o o |
| .E . . |
| o .. . |
| . . |
| . |
----[SHA256]-----
[rootcontrol ~]# 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) cant 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
root192.168.85.129s password: Number of key(s) added: 1Now 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的主要文件
rootlocalhost ~] tree /etc/ansible/
/etc/ansible/
├── ansible.cfg #主配置文件
├── hosts #主机清单文件 必须写
└── roles #公共角色文件[rootlocalhost ~] vim /etc/ansible/hosts
[liang] #组中两个被管理机的IP以及组名相当于组长组名名字可以随便取
192.168.16.135
192.168.16.136
没有组的主机要在有组的前面查看主机清单的主机结构
[rootcontrol ansible]# ansible-inventory --graph
all:|--ungrouped:|--webserver:| |--manged如果写的域名还需在/etc/hosts写配置 5.测试
[rootlocalhost ~] ansible all -m ping
#解释用于测试远程主机是否在线回复pong表示在线
#ping模块用于检查指定节点机器被管理主机是否还能连通
#用法简单不涉及参数主机如果在线则回复pong二、ansible常用模块 [rootcontrol ansible]# ansible-doc yum 查看模块用法 [rootlocalhost ~]# ansible-doc -l 查看ansibe中已有的模块 ansible调用格式 ansible [节点] -m [模块] -a [参数]
1.setup模块
[rootlocalhost ~]# ansible all -m setup 查看某个节点的信息 2.copy模块 该模块可实现从管理机向节点复制静态文件并设置合理的权限
参数 选项 含义
dest --- 文件复制的目的地
src --- 复制的源文件
backup yes/no 是否备份原始文件
validate - 复制前是否检验需要复制目的路径
mode --- 给文件加权限 使用ansible all -m copy -a ‘src ~ /xixi.txt dest~/ mode777’
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脚本发送到节点上执行。在实际的工作中经常会发送脚本控制远程主机执行指定任务所有该模块使用会频繁使用。
写一个脚本
[rootcontrol ~]# vim xi.sh
[rootcontrol ~]# cat xi.sh
!#/bin/bash
echo hello ansible
touch hell.txt
[rootcontrol ~]# ansible all -m script -a ~/xi.sh
在目标主机执行后出现该文件5、yum模块 yum模块能够从指定的服务器自动下载安装RPM包并且可以自动处理依赖性关系。
参数 选项 含义
name --- 包名
state present 安装默认latest 更新absent 卸载使用 ansible all -m yum -a “namevsftpd statepresent” 6.serice模块 service模块用来管理节点上的服务可开启关闭重启服务如httpdsshdnfsnamed等服务。
参数 选项 含义
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服务
示例httpd.yml
- hosts: websrvsremote_user: roottasks:- name: Install httpdyum: namehttpd statepresent- name: Install configure filecopy: srcfiles/httpd.conf dest/etc/httpd/conf/- name: start serviceservice: namehttpd statestarted enabledyes