1、實現ansible跟節點間無密碼訪問,不會配置的請看 文章 。php
2、建立目錄html
$ mkdir -p playbooks/{files,templates}
3、建立php測試文件index.phppython
$ vim playbooks/file/index.php
--------------------------------------------->mysql
<?php phpinfo() ?>
4、添加工做組group01linux
$ vim /etc/ansible/hosts
-------------------------------------->c++
[group01] 172.16.1.201 172.16.1.202
5、建立playbook文件:install_centos_lamp.ymlsql
$ vim playbooks/install_centos_lamp.yml
------------------------------------------------------------>vim
- hosts: group01 remote_user: root tasks:
- name: install yum packages
yum: pkg={{ item }}
sudo: True //sudo用於普通用戶,如用root,可省略此條命令
with_items:
- epel-release
- pcre
- gcc-c++
- zlib*
- mariadb
- httpd
- php
- php-mysql
- php-gd
- libjpeg*
- php-ldap
- php-odbc
- php-pear
- php-xml*
- php-mbstring
- php-bcmath
- php-mhash
- libselinux-python - name: copy index.php copy: src="files/index.php" dest="/var/www/html/index.php" //複製本地文件到節點指定目錄 tags: php notify: - server restart - name: server enable //設置開機啓動服務 service: name=mariadb state=started enabled=true - name: server start service: name=httpd state=started enabled=true handlers: - name: server restart //啓動服務 service: name=httpd state=restarted - name: server restart service: name=mariadb state=restarted
6、運行install_centos_lamp.ymlcentos
$ sudo ansible-playbook install_centos_lamp.yml
結果以下圖測試