ansible一键部署LAMP
一、实现ansible跟节点间无密码访问,不会配置的请看 文章 。
二、创建目录
$ mkdir -p playbooks/{files,templates}
三、创建php测试文件index.php
$ vim playbooks/file/index.php
--------------------------------------------->
<?php phpinfo() ?>
四、添加工作组group01
$ vim /etc/ansible/hosts
-------------------------------------->
[group01] 172.16.1.201 172.16.1.202
五、创建playbook文件:install_centos_lamp.yml
$ vim playbooks/install_centos_lamp.yml
------------------------------------------------------------>
- 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
六、运行install_centos_lamp.yml
$ sudo ansible-playbook install_centos_lamp.yml
结果如下图