使用playbook部署lamp环境

使用playbook部署lamp环境

推荐先看:Ansible基本配置使用

Playbook常用文件夹作用: 
files:存放需要同步到异地服务器的源码文件及配置文件; 
handlers:当服务的配置文件发生变化时需要进行的操作,比如:重启服务,重新加载配置文件;
meta:角色定义,可留空;
tasks:需要进行的执行的任务;   #任务
templates:用于执行lamp安装的模板文件,一般为脚本;
vars:本次安装定义的变量

安装lamp环境

[root@server ansible]# yum install httpd mariadb-server php php-mysql -y
[root@server ansible]# mkdir /data
[root@server ansible]# chown -R mysql:mysql /data/
[root@server ansible]# vim /etc/my.cnf
 [mysqld]
 datadir=/data
[root@server ansible]# systemctl start mariadb
创建一个测试页
[root@server ansible]# vim /var/www/html/index.php
<?php
phpinfo();
?>
[root@server ansible]# systemctl restart httpd
测试:http://192.168.220.138/index.php

配置playbook

[root@server ansible]# mkdir -p /etc/ansible/lamp/roles/{prepare,httpd,mariadb,php}/{tasks,files,templates,vars,meta,default,handlers}
[root@server ansible]# tree /etc/ansible/lamp/
/etc/ansible/lamp/
└── roles
    ├── httpd
    │   ├── default
    │   ├── files
    │   ├── handlers
    │   ├── meta
    │   ├── tasks
    │   ├── templates
    │   └── vars
    ├── mariadb
    │   ├── default
    │   ├── files
    │   ├── handlers
    │   ├── meta
    │   ├── tasks
    │   ├── templates
    │   └── vars
    ├── php
    │   ├── default
    │   ├── files
    │   ├── handlers
    │   ├── meta
    │   ├── tasks
    │   ├── templates
    │   └── vars
    └── prepare
        ├── default
        ├── files
        ├── handlers
        ├── meta
        ├── tasks
        ├── templates
        └── vars

拷贝配置文件        
[root@server ansible]# cp /etc/httpd/conf/httpd.conf lamp/roles/httpd/files/
[root@server ansible]# cp /etc/my.cnf lamp/roles/mariadb/files/

1.构建httpd任务
[root@server ~]# cd /etc/ansible/lamp/roles/httpd/
[root@server httpd]# mv /var/www/html/index.php files/
[root@server httpd]# vim tasks/main.yml
- name: install httpd
  yum: name=httpd state=present
- name: provide test page
  copy: src=index.php dest=/var/www/html/
- name: provide configuration file
  copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  notify: restart httpd
[root@server httpd]# vim handlers/main.yml
- name: restart httpd
  service: name=httpd enabled=yes state=restarted

2.构建mariadb任务
[root@server ~]# cd /etc/ansible/lamp/roles/mariadb/
- name: install mariadb
  yum: name=mariadb-server state=present
- name: mkdir data DIR
  shell: mkdir /data
- name: provide configuration files
  copy: src=my.cnf dest=/etc/my.cnf
- name: change privileges
  shell: chown -R mysql:mysql /data/
- name: start mariadb
  service: name=mariadb enabled=yes state=started


3.构建php任务
[root@server ~]# cd /etc/ansible/lamp/roles/php/
[root@server php]# vim tasks/main.yml
- name: install php
  yum: name=php state=present
- name: install php-mysql
  yum: name=php-mysql state=present
 
4。定义site.yml
[root@server ~]# cd /etc/ansible/lamp/roles/
[root@server roles]# ls
httpd  mariadb  php  prepare
- name: build LAMP
  remote_user: root
  hosts: client
  roles:
    - mariadb
    - php
    - httpd


[root@server roles]# ansible-playbook -i /etc/ansible/hosts ./site.yml 

PLAY [build LAMP] *****************************************************************

TASK [Gathering Facts] ************************************************************
ok: [192.168.220.139]

TASK [mariadb : install mariadb] **************************************************
changed: [192.168.220.139]

TASK [mariadb : mkdir data DIR] ***************************************************
 [WARNING]: Consider using the file module with state=directory rather than
running 'mkdir'.  If you need to use command because file is insufficient you can
add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.

changed: [192.168.220.139]

TASK [mariadb : provide configuration files] **************************************
ok: [192.168.220.139]

TASK [mariadb : change privileges] ************************************************
 [WARNING]: Consider using the file module with owner rather than running 'chown'.
If you need to use command because file is insufficient you can add 'warn: false'
to this command task or set 'command_warnings=False' in ansible.cfg to get rid of
this message.

changed: [192.168.220.139]

TASK [mariadb : start mariadb] ****************************************************
changed: [192.168.220.139]

TASK [php : install php] **********************************************************
changed: [192.168.220.139]

TASK [php : install php-mysql] ****************************************************
ok: [192.168.220.139]

TASK [httpd : install httpd] ******************************************************
ok: [192.168.220.139]

TASK [httpd : provide test page] **************************************************
ok: [192.168.220.139]

TASK [httpd : provide configuration file] *****************************************
changed: [192.168.220.139]

RUNNING HANDLER [httpd : restart httpd] *******************************************
changed: [192.168.220.139]

PLAY RECAP ************************************************************************
192.168.220.139            : ok=12   changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
发布了65 篇原创文章 · 获赞 48 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/DoloresOOO/article/details/99820419