Ansible部署LAMP

Ansible部署LAMP

文章目录

  • Ansible部署LAMP
    • 环境说明
    • 准备工作
      • 1. 为ansible主机安装epel源
      • 2. 安装ansible
      • 3. 创建ansible清单
      • 4. 测试ansible主机与受管主机是否互通
    • 编写playbook
    • 编写index.php文件
    • 执行playbook
    • 测试

这里我们采用循环变量的方式来部署LAMP架构

环境说明

IP 主机名 备注
192.168.100.100 node1 ansible主机
192.168.100.110 node2 受管主机

准备工作

1. 为ansible主机安装epel源

[root@node1 ~]# yum -y install epel-release
......略
Installed:
  epel-next-release-8-11.el8.noarch                                   epel-release-8-11.el8.noarch                                  

Complete!

2. 安装ansible

[root@node1 ~]# yum -y install ansible
......略
Installed:
  ansible-2.9.23-1.el8.noarch                          libsodium-1.0.18-2.el8.x86_64        python3-babel-2.5.1-6.el8.noarch       
  python3-bcrypt-3.1.6-2.el8.1.x86_64                  python3-cffi-1.11.5-5.el8.x86_64     python3-cryptography-3.2.1-5.el8.x86_64
  python3-jinja2-2.10.1-3.el8.noarch                   python3-jmespath-0.9.0-11.el8.noarch python3-markupsafe-0.23-19.el8.x86_64  
  python3-paramiko-2.4.3-1.el8.noarch                  python3-pip-9.0.3-20.el8.noarch      python3-ply-3.9-9.el8.noarch           
  python3-pyasn1-0.3.7-6.el8.noarch                    python3-pycparser-2.14-14.el8.noarch python3-pynacl-1.3.0-5.el8.x86_64      
  python3-pytz-2017.2-9.el8.noarch                     python3-pyyaml-3.12-12.el8.x86_64    python3-setuptools-39.2.0-6.el8.noarch 
  python36-3.6.8-37.module_el8.5.0+771+e5d9a225.x86_64 sshpass-1.06-9.el8.x86_64           

Complete!

3. 创建ansible清单

[root@node1 ~]# mkdir /opt/httpd
[root@node1 ~]# cd /opt/httpd
[root@node1 httpd]# vim inventory
[httpd]
192.168.100.110

4. 测试ansible主机与受管主机是否互通

[root@node1 httpd]# ansible all -m ping
192.168.100.110 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

至此,准备工作已经完成,下面开始部署LAMP

编写playbook

[root@node1 lamp]# mkdir playbook
[root@node1 lamp]# cd playbook/
[root@node1 playbook]# vim install_lamp.yml
---
- hosts: 192.168.100.110
  gather_facts: no
  vars: 
    packages: 
      - httpd
      - mariadb
      - mariadn-server
      - php*
    service: 
      - httpd
      - mariadb
      - php-fpm
  tasks: 
    - name: stop firewalld
      service: 
        name: firewalld
        state: stopped
        enabled: no

    - name: configure selinux
      lineinfile: 
        path: /etc/selinux/config
        regexp: '^SELINUX='
        line: SELINUX=disabled

    - name: install httpd,mysql,php
      yum: 
        name: "{{ item }}"
        state: present
      loop: 
        "{{ packages }}"

    - name: copy file
      copy: 
        src: /opt/lamp/file/index.php
        dest: /var/www/html
    
    - name: service restart
      service: 
        name: "{{ item }}"
        state: restarted
        enabled: yes
      loop: 
        "{{ service }}"      

编写index.php文件

[root@node1 ~]# vim /opt/lamp/file/index.php

执行playbook

[root@node1 playbook]# ansible-playbook install_lamp.yml 

PLAY [192.168.100.110] **************************************************************************************************************

TASK [stop firewalld] ***************************************************************************************************************
ok: [192.168.100.110]

TASK [configure selinux] ************************************************************************************************************
ok: [192.168.100.110]

TASK [install httpd,mysql,php] ******************************************************************************************************
ok: [192.168.100.110] => (item=httpd)
ok: [192.168.100.110] => (item=mariadb)
changed: [192.168.100.110] => (item=mariadb-server)
ok: [192.168.100.110] => (item=php*)

TASK [copy file] ********************************************************************************************************************
changed: [192.168.100.110]

TASK [service restart] **************************************************************************************************************
changed: [192.168.100.110] => (item=httpd)
changed: [192.168.100.110] => (item=mariadb)
changed: [192.168.100.110] => (item=php-fpm)

PLAY RECAP **************************************************************************************************************************
192.168.100.110            : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@node1 playbook]# 

测试

Ansible部署LAMP_第1张图片

你可能感兴趣的:(lamp,linux)