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

猜你喜欢

转载自www.cnblogs.com/lsgo/p/10422576.html