ansible利用playbook 部署lamp架构

搭建参考:ansible批量运维管理-CSDN博客

定义ansible主机清单

[root@ansible-server ~]# vim /etc/hosts
192.168.200.129 host01
192.168.200.130 host02
[root@ansible-server ~]# vim /etc/ansible/hosts 
[webserver]
host01
host02

在ansible端编写index.html,index.php⽂件,及lamp.yml配置⽂件

[root@ansible-server ~]# mkdir playbooklamp
[root@ansible-server ~]# cd playbooklamp/
[root@ansible-server playbooklamp]# vim index.html
[root@ansible-server playbooklamp]# cat index.html 
<html>
        <head>
                <title> 这个一个测试页面</title>
                <meta charset="utf-8">
                <head>
                <body> 
                这是一个ansible测试页面!!!
                </body>
        </html>
[root@ansible-server playbooklamp]# vim index.php
[root@ansible-server playbooklamp]# cat index.php 
<?php
phpinfo();
?>
[root@ansible-server playbooklamp]# vim lamp.yml  
[root@ansible-server playbooklamp]# cat lamp.yml 
- hosts: 'webserver'
  tasks:

    - name: "安装lamp需要的软件包"
      yum: 
        name: "{{item}}"
        state: installed
      with_items:
        - apr
        - apr-util
        - httpd
        - httpd-devel
        - mariadb
        - mariadb-server
        - php
        - php-mysqlnd
        - php-fpm
    - name: "传输index.html文件"
      copy:
        src: ./index.html
        dest: /var/www/html/index.html
        owner: root
        group: root
        mode: 0644
    - name: "传送index.php文件"
      copy: 
        src: ./index.php
        dest: /var/www/html/index.php
        owner: root
        group: root
        mode: 0644
    - name: "重启httpd"
      service:
        name: httpd
        state: restarted
    - name: "重启mariadb"
      service:
        name: mariadb
        state: restarted
    - name: "重启php-fpm"
      service:
        name: php-fpm
        state: restarted
    - name: "关闭防火墙和selinux"
      block:
        - shell: systemctl stop firewalld
          ignore_errors: yes
        - name: "关闭Selinux"
          shell: setenforce 0
          ignore_errors: yes
[root@ansible-server playbooklamp]# ansible-playbook  lamp.yml --syntax-check 

playbook: lamp.yml
[root@ansible-server playbooklamp]#

运行剧本

[root@ansible-server playbooklamp]# ansible-playbook  lamp.yml 

PLAY [webserver] **************************************************************************************

TASK [Gathering Facts] ********************************************************************************
ok: [host02]
ok: [host01]

TASK [安装lamp需要的软件包] ***************************************************************************
ok: [host01] => (item=apr)
ok: [host02] => (item=apr)
ok: [host01] => (item=apr-util)
ok: [host02] => (item=apr-util)
ok: [host01] => (item=httpd)
ok: [host02] => (item=httpd)
ok: [host01] => (item=httpd-devel)
ok: [host02] => (item=httpd-devel)
ok: [host01] => (item=mariadb)
ok: [host02] => (item=mariadb)
ok: [host01] => (item=mariadb-server)
ok: [host02] => (item=mariadb-server)
ok: [host01] => (item=php)
ok: [host02] => (item=php)
changed: [host01] => (item=php-mysqlnd)
changed: [host02] => (item=php-mysqlnd)
ok: [host01] => (item=php-fpm)
ok: [host02] => (item=php-fpm)

TASK [传输index.html文件] *****************************************************************************
changed: [host01]
changed: [host02]

TASK [传送index.php文件] ******************************************************************************
changed: [host02]
changed: [host01]

TASK [重启httpd] **************************************************************************************
changed: [host02]
changed: [host01]

TASK [重启mariadb] ************************************************************************************
changed: [host02]
changed: [host01]

TASK [重启php-fpm] ************************************************************************************
changed: [host01]
changed: [host02]

TASK [shell] ******************************************************************************************
changed: [host01]
changed: [host02]

TASK [关闭Selinux] ************************************************************************************
changed: [host01]
changed: [host02]

PLAY RECAP ********************************************************************************************
host01                     : ok=9    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host02                     : ok=9    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible-server playbooklamp]# 

浏览器访问测试

相关推荐
学Linux的语莫5 小时前
Ansible使用简介和基础使用
linux·运维·服务器·nginx·云计算·ansible
学Linux的语莫13 小时前
Ansible Playbook剧本用法
linux·服务器·云计算·ansible
Nightwish514 小时前
ansible操作随记(一)
ansible
qq_3831398418 小时前
ansible playbook安装nacos
ansible
Aimyon_365 天前
⾃动化运维利器 Ansible-Jinja2
运维·ansible
柒月VII6 天前
【Ansible常用命令+模块+Playbook+Roles】
linux·服务器·ansible
Linux运维技术栈6 天前
生产环境centos8 & Red Hat8部署ansible and 一键部署mysql两主两从ansible脚本预告
运维·数据库·mysql·自动化·ansible
Aimyon_366 天前
⾃动化运维利器 Ansible-最佳实战
linux·运维·ansible
饭桶也得吃饭6 天前
运维工具Ansible部署、配置
运维·服务器·ansible
陪小七许个愿6 天前
Ansible一键部署Kubernetes集群
容器·kubernetes·ansible