1. 程式人生 > >ansible-playbook案例

ansible-playbook案例

apachectl cal 要求 默認 line ice pla ner list

本案例要求:

  • 安裝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案例