ansible实战__使用yum方法安装nginx
目录结构
[root@master yum_install_nginx]# tree
.
├── CentOS-Base.repo
└── nginx_install.yaml
如果yum源有问题需要建 CentOS-Base.repo文件,没有则不需要
报错信息可参考如下链接:
CentOS-Base.repo文件内容
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS
# updates unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base - Aliyun
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - Aliyun
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - Aliyun
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - Aliyun
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
注释区域就是为了解决yum源问题,没有则不需要打开
nginx_install.yaml内容
- hosts: node2
tasks:
#- name: replace aliyun
# copy: src=./CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo backup=yes mode=644
#- name: clean all
# command: yum clean all
#- name: makecache
# command: yum makecache
#- name: install epel-release
# yum: name=epel-release state=latest
#- name: update yum
# command: yum update
- name: install nginx
yum:
name: nginx
state: latest
- name: start nginx
systemd: #或者使用service
name: nginx
state: started
enabled: yes
- name: start firewall 80
firewalld:
port: 80/tcp
permanent: yes
immediate: yes
state: enabled
检查yml文件格式是否正确,没有报错则没问题
[root@master yum_install_nginx]# ansible-playbook -i ../hosts nginx_install.yaml --syntax-check
playbook: nginx_install.yaml
查看操作哪些机器节点
[root@master yum_install_nginx]# ansible-playbook -i ../hosts nginx_install.yaml --list-hosts
playbook: nginx_install.yaml
play #1 (node2): node2 TAGS: []
pattern: [u'node2']
hosts (1):
node2
执行,注意yaml文件没有和主机清单文件在同一目录,需要指定主机清单文件路径
[root@master yum_install_nginx]# ansible-playbook -i ../hosts nginx_install.yaml
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!