目录结构
[root@master yum_install_nginx]# tree
.
├── CentOS-Base.repo
└── nginx_install.yaml
如果yum源有问题需要建 CentOS-Base.repo文件,没有则不需要
报错信息可参考如下链接:
https://blog.csdn.net/2401_82700290/article/details/140707069
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