Ansible安装以及常用模块操作(1)
第一章ansible安装介绍
ansible基础架构环境规划
主机名 | 外网IP | 内网IP |
ansible控制端 | 122.112.168.83 | |
ansible被控端 | 121.36.10.238 | |
ansible被控端 | 103.80.27.77 |
ansible 特点
1.容易学习,无代理模式,不像saltstack既要学服务端又要学习客户端,还要学习服务端与客户端之间的通讯协议
2.操作灵活,体现在Ansible有较多的模块,提供了丰富的功能,playbook则提供了类似于编程语言的复杂功能
3.简单易用,体现在Ansible —个命令可以完成很多事情
4.安全可靠,因为Ansible使用了SSH协议进行通汛,既稳定又安全
5.可移植性高,可以将写好的playbook拷贝至任意机器进行执行
4.Ansible架构中的控制节点、被控制节点、inventroy, ad-hoc、playbook、连接协议是什么?
1.ansible-server安装epel源
[root@blog ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@blog ~]# yum -y install ansible
2.ansible版本
[root@blog ~]# ansible --version
ansible 2.9.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
3.Ansible的配置文件,配置文件可以随意放,但有查找顺序
$ANSIBLE_CONFIGansible.cfg #当前目录下面查找
.ansible.cfg #当前用户的家目录下查找
/etc/ansible/ansible.cfg
#inventory = /etc/ansible/hosts #主机列表配置文件
#library = /usr/share/my_modules/ #库文件存放目录
#remote_tmp = ~/.ansible/tmp #临时py文件存放在远程主机目录
#local_tmp = ~/.ansible/tmp #本机的临时执行目录
#forks = 5 #默认并发数
#sudo_user = root #默认sudo用户
#ask_sudo_pass = True #每次执行是否询问sudo的ssh密码
#ask_pass = True #每次执行是否询问ssh密码
#remote_port = 22 #远程主机端口
host_key_checking = False #跳过检查主机指纹
log_path = /var/log/ansible.log #ansible日志
[privilege_escalation] #如果是普通用户则需要配置提权
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False
第二章 Ineventory主机清单
2.1基于密码连接
#方式一、主机+端口+密码
[webservers]
172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=‘123456'
172.16.1.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=‘123456'
#方式二、主机+端口+密码[webservers]
web[1:2].oldboy.com ansible_ssh_pass=‘123456'
#方式三、主机+端口+密码
[webservers]
web[1:2].oldboy.com
[webservers:vars] ansible_ssh_pass='123456'
2.2.基于密钥连接,需要先创建公钥和私钥,并下发公钥至被控端
[root@blog ~]# #ssh-keygen
[root@blog ~]# # ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@m01 ~]# cat hosts
#方式一、主机+端口+密钥
[webservers]
172.16.1.7
172.16.1.8
2.3.场景三、主机组使用方式
#1.定义两个组
[lbservers]
172.16.1.5
172.16.1.6
[webservers]
172.16.1.7
172.16.1.8
2.servers组包括两个子组[lbservers,webserver]
[servers:children]
[lbservers]
[webserver]
#列出当前某个组有多少台主机
[root@blog ~]# ansible slbserver -m ping -i ./hosts
121.36.10.238 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@blog ~]# ansible webserver -m ping -i ./hosts
103.80.27.77 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
注意:
如果控制端和被控制端第一次通讯,需要先添加指纹信息,那如果机器特别多少的情况下怎么办?
仅需开启ansible中的 host_key_checking = False
第三章 Ad-Hoc
3.1.常用模块
command 执行shell命令(不支持管道等特殊字符)
shell 执行shell命令
scripts 执行shell脚本
yum_repository 配置yum仓库
get_url 联网下载
yum 安装
copy 配置
service、systemd 启动
user、group 创建用户与组
file 授权
crond 定时任务
mount 挂载
firewalld firewall
selinux selinux
3.2.使用过程中需要先了解ansible-doc帮助手册
[root@blog ~]# ansible-doc -l 查看所有模块说明
[root@blog ~]# ansible-doc copy 表示查看指定模块说明
[root@blog ~]# ansible-doc -s copy 表示指定模块的参数
3.3command默认执行bash命令模块,模块不支持重定向或管道
[root@blog ~]# ansible webserver -a "hostname" -i ./hosts
zabbix-agent01 | CHANGED | rc=0 >>
zabbix-agent
ansible | CHANGED | rc=0 >>
anslbes
3.4.shell模块,如果需要一些管道操作,则使用shell
[root@blog ~]# ansible webserver -m shell -a "ifconfig|grep eth0" -f 50 -i ./hosts
ansible | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
zabbix-agent01 | CHANGED | rc=0 >>
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
3.5.script脚本模块
[root@blog ~]# cat 1.sh
yum -y install htop
[root@blog ~]# ansible webserver -m script -a "/root/1.sh" -i ./hosts
3.6.yum安装软件模块
[root@blog ~]# ansible webserver -m yum -a "name=httpd state=installed" -i ./hosts
Name
httpd #指定要安装的软件包名称
file:// #指定从本地哪个目录安装
rpm http:// #指定从哪个网站安装rpm包
state #指定使用yum的方法
present #安装软件包
absent #移除软件包
latest #安装最新软件包
list=ansible #列出当前仓库可用的软件包
disablerepo="epel,ol7_latest" #安装软件时,不从哪些仓库获取
download_only=true #仅下载软件包,不安装
3.7.copy文件拷贝模块
#1.拷贝文件文件至被控节点
[root@blog ~]# ansible webserver -m copy -a "src=/root/1.sh dest=/tmp/1.sh" -i ./hosts
#2.对远端已有文件进行备份,首先是文件有变化,按照时间信息备份
[root@blog ~]# ansible webserver -m copy -a "src=/root/1.sh dest=/tmp/1.sh backup=yes" -i ./hosts
#3.向被控端主机写入数据,并且会覆盖远端文件内原有数据信息
[root@blog ~]# ansible webserver -m copy -a "content='ansible' dest=/tmp/1.sh" -i ./hosts
3.8.file文件创建模块
1.直接修改被控端的权限
[root@blog ~]# ansible webserver -m file -a "path=/tmp/1.sh mode=0400" -i ./hosts
2. 在远程主机创建目录
[root@blog ~]# ansible webserver -m file -a "path=/tmp/opt state=directory" -i ./hosts
3.在被控端创建文件
[root@blog ~]# ansible webserver -m file -a "path=/tmp/test state=touch mode=555 owner=root group=root" -i ./hosts
4.递归授权目录权限
[root@blog ~]# ansible webserver -m file -a "path=/tmp owner=zabbix group=zabbix recurse=yes" -i ./hosts
3.8.文件下载模块
下载软件包
[root@blog ~]# ansible webserver -m get_url -a "url=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/2.2/rhel/7/x86_64/zabbix-2.2.12-1.el7.x86_64.rpm dest=/tmp mode=0777" -i ./hosts
3.9.启动service模块
启动crond并加入开机自启动
[root@blog ~]# ansible webserver -m service -a "name=crond state=started enabled=yes" -i ./hosts
停止crond服务,删除开机自启动
[root@blog ~]# ansible webserver -m service -a "name=crond state=stopped enabled=no" -i ./hosts
重启crond服务
[root@blog ~]# ansible webserver -m service -a "name=crond state=restarted" -i ./hosts
重新加载crond服务
[root@blog ~]# ansible webserver -m service -a "name=crond state=reloaded" -i ./hosts
3.10.group组模块
创建远程主机组
[root@blog ~]# ansible webserver -m group -a "name=test gid=124" -i ./hosts
移除远程组
[root@blog ~]# ansible webserver -m group -a "name=test state=absent" -i ./hosts
3.11.user模块
1.创建用户并指定uid和gid 不允许登录也不创建家目录
[root@blog ~]# ansible webserver -m user -a "name=ssebank uid=996 group=996 shell=/sbin/nologin create_home=no" -i ./hosts
2.删除用户
[root@blog ~]# ansible webserver -m user -a "name=ssebank state=absent" -i ./hosts
3.12.crond模块
1.添加一条定时任务
[root@blog ~]# ansible webserver -m cron -a "name=test minute=* hour=* day=* month=* weekday=* job='/bin/bash /server/scripts/test.sh'" -i ./hosts
[root@blog ~]# ansible webserver -m cron -a "job='/bin/bash /server/scripts/test.sh'" -i ./hosts
2.设置定时任务并name命名防止重复
[root@blog ~]# ansible webserver -m cron -a "name=ansible job='/bin/bash /server/scripts/test.sh'" -i ./hosts
3.删除响应的定时任务
[root@blog ~]# ansible webserver -m cron -a "name='ansible' minute=0 hour=0 job='/bin/bash /server/scripts/test.sh' state=absent" -i ./hosts
4.注释响应的定时任务
[root@blog ~]# ansible webserver -m cron -a "name='test' minute=0 hour=0 job='/bin/bash /server/scripts/test.sh' disabled=yes" -i ./hosts
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端