首页 > 其他 > 详细

ansible-playbook案例

时间:2019-02-23 14:43:09      阅读:186      评论:0      收藏:0      [点我收藏+]

本案例要求:

  • 安装Apache并修改监听端口为8080
  • 修改ServerName配置,执行apachectl -t命令不报错
  • 设置默认主页hello world
  • 启动服务并设开机自启

   

---
- hosts: cache
  remote_user: root
  tasks:
    - name: install one specific version of Apache
      yum:
        name: httpd        //安装Apache
        state: installed
    - lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: ^Listen 
        line: Listen 8080        //修改端口为8080
    - replace:
        path: /etc/httpd/conf/httpd.conf
        regexp: ^#(ServerName).*        //配置ServerName
        replace: \1 localhost
    - service:
        name: httpd
        enabled: yes        //开机自启
        state: restarted
    - copy:
        src: /root/index.html        //修改主页,可以自己写个页面
        dest: /var/www/html/index.html

 

扩展的一个tags和notify以及handlers

---
- hosts: web1
  remote_user: root
  tasks:
    - name: install the latest version of Apache
      yum:
        name: httpd
        state: latest
    - replace:
        path: /etc/httpd/conf/httpd.conf
        regexp: (^Listen).*
        replace: \1 8080
        backup: yes
    - replace:
        path: /etc/httpd/conf/httpd.conf
        regexp: (^#ServerName ).*
        replace: \1 localhost
        backup: yes
    - copy:
        src: test1.yml
        #src: /root/index.html
        dest: /root/user13.yml
        #dest: /var/www/html
        owner: apache
        group: apache
        mode: 0644
      tags: haha
      notify:
        - restart httpd
        - restart vsftpd
        - modify port
  handlers:
    - name: modify port
      replace:
        path: /etc/httpd/conf/httpd.conf
        regexp: (^Listen ).*
        replace: \1 80
        backup: yes
    - name: restart httpd
      service: name=httpd state=restarted
    - name: restart vsftpd
      service: name=vsftpd state=restarted
    - service:
        name: httpd
        state: restarted
        enabled: yes

 

注意格式要求,一般的copy,replace,yum,sevice模块用ansible-doc 模块名  查看帮助就ok

ansible-playbook案例

原文:https://www.cnblogs.com/lsgo/p/10422576.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!