Operations 11 min read

How to Automate LAMP Stack Deployment with Ansible Roles

This guide walks you through using Ansible—an agent‑less, SSH‑based automation tool—to create reusable roles and deploy a complete LAMP platform across multiple servers, covering installation, inventory setup, SSH key authentication, directory structure, playbooks, tasks, handlers, and verification steps.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Automate LAMP Stack Deployment with Ansible Roles
Introduction When operations engineers need to modify a parameter or deploy a platform on dozens or hundreds of servers, Ansible provides a powerful solution. Built on Python, Ansible combines the advantages of tools like Puppet, Chef, and Fabric, offering agent‑less, server‑less, YAML‑based automation for bulk configuration, deployment, and command execution. Ansible Overview Features No agents: no client software required on managed hosts. No server: run commands directly. Modules in any language: extend functionality with custom modules. YAML, not code: write playbooks in easy‑to‑read YAML. SSH by default. Strong multi‑tier solution. Basic Architecture Command Syntax <code>#常用格式 ansible &lt;host-pattern&gt; [-f forks] [-m module] [-a args] host-pattern # 可以是all,或者配置文件中的主机组名 -f forks # 指定并行处理的进程数 -m module # 指定使用的模块,默认模块为command -a args # 指定模块的参数 #查看各模块 ansible-doc [options] [modules] # 主要选项有: -l或--list # 列出可用的模块 -s或--snippet # 显示指定模块的简略使用方法</code> For detailed information, refer to the official documentation. Ansible Role for LAMP Batch Deployment What is an Ansible Role? Roles, introduced in Ansible 1.2, allow hierarchical, structured organization of playbooks. A role automatically loads its variables, tasks, handlers, files, templates, and meta data. Using include in a playbook, you can invoke a role, which isolates each component into its own directory. Creating a Role <code>#创建role的步骤 (1) 创建以roles命名的目录; (2) 在roles目录中分别创建以各角色名称命名的目录,如webservers等; (3) 在每个角色命名的目录中分别创建files、handlers、meta、tasks、templates和vars目录;用不到的目录可以创建为空目录,也可以不创建; (4) 在playbook文件中,调用各角色; #role内各目录中可用的文件 tasks目录:至少应该包含一个名为main.yml的文件,其定义了此角色的任务列表;此文件可以使用 include 包含其它的task文件; files目录:存放由copy或script等模块调用的文件; templates目录:template模块会自动在此目录中寻找Jinja2模板文件; handlers目录:此目录中应当包含一个main.yml文件,用于定义此角色用到的各handler;在handler中使用include包含的其它handler文件也应该位于此目录中; vars目录:应当包含一个main.yml文件,用于定义此角色用到的变量; meta目录:应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系; default目录:为当前角色设定默认变量时使用此目录;应当包含一个main.yml文件;</code> Experimental Topology Configuration Process Install Ansible <code>[root@scholar ~] # yum install ansible -y #需epel源</code> Configure inventory file <code>[root@scholar ~] # vim /etc/ansible/hosts #定义被控主机 [webservers] 172.16.10.123 ansible_ssh_user=root ansible_ssh_pass=centos 172.16.10.124 ansible_ssh_user=root ansible_ssh_pass=centos [dbservers] 172.16.10.125 ansible_ssh_user=root ansible_ssh_pass=centos</code> Set up SSH key authentication <code>[root@scholar ~] # ssh-keygen -t rsa -P '' [root@scholar ~] # yum install sshpass -y #请确保安装sshpass,不然无法通信</code> Create role directories <code>[root@scholar ~] # mkdir lamp/role -pv [root@scholar role] # mkdir web/{files,handlers,meta,tasks,templates,vars,default} db/{files,handlers,meta,tasks,templates,vars,default} php/{files,handlers,meta,tasks,templates,vars,default} -p</code> Prepare service configuration files <code>[root@scholar role] # cp /etc/httpd/conf/httpd.conf web/files/ [root@scholar role] # cp /etc/php.ini php/files/ [root@scholar role] # cp /etc/my.cnf db/files/</code> Create playbooks and role files <code>[root@scholar role] # touch web.yml php.yml db.yml site.yml # touch web/{handlers,tasks}/main.yml db/{handlers,tasks}/main.yml php/tasks/main.yml # vim web.yml - name: web service remote_user: root hosts: webservers roles: - web # vim php.yml - name: php service remote_user: root hosts: webservers roles: - php # vim db.yml - name: mysql service remote_user: root hosts: dbservers roles: - db</code> Define tasks and handlers <code># web/tasks/main.yml - name: install httpd yum: name=httpd state=present - name: configuration httpd copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf notify: - restart httpd - name: service httpd start service: name=httpd enabled=no state=started # web/handlers/main.yml - name: restart httpd service: name=httpd state=restarted # php/tasks/main.yml - name: install php yum: name=php state=present - name: configuration php copy: src=php.ini dest=/etc/php.ini # db/tasks/main.yml - name: install mysql yum: name=mysql state=present - name: install mysql-server yum: name=mysql-server state=present - name: configuration mysqld copy: src=my.cnf dest=/etc/my.cnf notify: - restart mysqld - name: service mysqld start service: name=mysqld enabled=no state=started # db/handlers/main.yml - name: restart mysqld service: name=mysqld state=restarted</code> Batch Deployment Deploy httpd, php, and mysql using the playbooks, then verify that the services are listening on the expected ports. Check that each node's service ports are listening. Conclusion Using Ansible roles to automate LAMP platform deployment is straightforward and scalable. This tutorial covers the basics; more complex deployments can be built on the same principles. Feel free to leave comments for any issues encountered.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Server ConfigurationAnsibleRolesLAMP
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.