ansible 中利用playbook部署lamp環境
基本檔案目錄如下:
httpd所需軟體:httpd mariadb mariadb-server php php-mysql gd php-gd
[[email protected] role]# ls httpd site_nginx.retry site.retry mariadb php-apache site_apache.yaml
[[email protected] role]# cat site_apache.yaml --- - hosts: group roles: - httpd - php-apache - mariadb
1.httpd目錄
[[email protected] role]# cd httpd/ [[email protected] httpd]# ls files handlers tasks templates vars
[[email protected] httpd]# cat files/index.html hello world
[[email protected] httpd]# cat handlers/main.yaml --- - name: reload httpd.service service: name=httpd state=reloaded
[[email protected] httpd]# cat files/index.html hello world [[email protected] httpd]# cat handlers/main.yaml --- - name: reload httpd.service service: name=httpd state=reloaded [[email protected] httpd]# cat tasks/main.yaml --- - name: install httpd yum: name=httpd state=present
- name: copy httpd configure file template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf notify: reload httpd.service
- name: copy index.html copy: src=index.html dest=/var/www/html/index.html
- name: start and enable httpd service: name=httpd state=started enabled=yes
[[email protected] httpd]# ls templates/ httpd.conf.j2
在httpd.conf.j2中定義變數
Listen {{ httpd_port }}
[[email protected] httpd]# cat vars/main.yaml httpd_port: 80
2.mariadb目錄
[[email protected] role]# cd mariadb/ [[email protected] mariadb]# ls handlers tasks templates vars //mariadb 不需要測試首頁可以不寫
[[email protected] mariadb]# cat handlers/main.yaml --- - name: restart mariadb.service service: name=mariadb state=restarted
[[email protected] mariadb]# cat handlers/main.yaml --- - name: restart mariadb.service service: name=mariadb state=restarted [[email protected] mariadb]# cat tasks/main.yaml --- - name: install mariadb yum: name={{ item }} state=present with_items: - mariadb - mariadb-server
- name: copy mariadb configure file template: src=my.cnf.j2 dest=/etc/ notify: restart mariadb.service
- name: start and enable mariadb service: name=mariadb state=started enabled=yes
[[email protected] mariadb]# ls templates/ my.cnf.j2
[[email protected] mariadb]# cat vars/main.yaml datadir: /var/lib/mysql socket: /var/lib/mysql/mysql.sock
3.apache中php目錄
[[email protected] php-apache]# ls files handlers tasks templates
[[email protected] php-apache]# cat handlers/main.yaml --- - name: restart httpd service: name=httpd state=restarted
[[email protected] php-apache]# cat handlers/main.yaml --- - name: restart httpd service: name=httpd state=restarted [[email protected] php-apache]# cat tasks/main.yaml --- - name: install php-fpm yum: name={{ item }} state=present with_items: - php - php-gd - php-mysql - gd
- name: copy php configure file template: src=php.ini.j2 dest=/etc/php.ini notify: restart httpd
- name: copy index.php copy: src=index.php dest=/var/www/html/index.php notify: restart httpd
[[email protected] php-apache]# ls templates/ php.ini.j2 //不定義變數可以不建立vars目錄