ansible实战__使用yum方法安装nginx

目录结构

[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
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值