天下風雲出我輩
一入江湖歲月催

Ansible-Playbook部署Nginx

编写Playbook部署nginx,等于就是把shell脚本模块化

hosts文件

[webserver]

192.168.1.230
192.168.1.231
按实际生产环境修改部署
---

- hosts: webserver

    vars:
    master_conf:/etc/nginx/nginx.conf
    domain_conf:/etc/nginx/conf.d/default.conf
  remote_user:root
  tasks:

  - name: "配置nginx官方安装源"
    yum_repository:
    name:nginx
    description:nginx yum_repository
    baseurl:http://nginx.org/packages/centos/7/$basearch/
    gpgcheck:0
    enabled:yes

  - name: "安装Nginx,tree,lrzsz软件包"

    yum:name={{ item }} state=present
    with_items:
    - nginx
    - tree
    - lrzsz

  - name: "分发配置文件"
    copy:
      src:"{{ item.src }}"
      dest:"{{ item.dest }}"
      owner:root
      group:root
      mode:0644
      backup:yes
    with_items:
      - { src: "{{ master_conf }}", dest: "{{ master_conf }}" }
      - { src: "{{domain_conf}}", dest: "{{ domain_conf }}" }
    notify:reload nginx
    tags:configfile#任务标记名configfile

  - name: "写入index.html测试访问"

    shell:echo "I am $(hostname)" > /usr/share/nginx/html/index.html

  - name: "开启nginx服务"
    service:name=nginx state=started enabled=yes

  handlers:
  - name: reload nginx
    service:name=nginx state=reloaded
检测文件语法
ansible-playbook --syntax-check  nginx_install.yml
赞(2) 打赏
未经允许不得转载:Anonym0x1 » Ansible-Playbook部署Nginx

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏