ansible-playbook案例

本案例要求:

  • 安装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

转载于:https://www.cnblogs.com/lsgo/p/10422576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值