热门课程

免费试听

上课方式

开班时间

当前位置: 首页 -   文章 -   新闻动态 -   正文

Ansible实现远程主机LAMP的构建

知了堂姐
2024-07-08 17:22:16
0

ansible命令:用于单个模块、任务的执行。

ansible-playbook:用来执行脚本、多个模块、任务(任务)

- name:说明信息。

modile:参数。

Playbokk是一个包含许多play的列表。

play的主要功能是将预先归入一组主机,并通过ansible中的task预先定义角色。

在/etc/ansible/role目录下

/{httpd,mysql,php}三个角色,如{tasks|template|var|meta|files等

Files:存放源代码文件和需要同步到异地服务器的配置文件。

Handlers:当服务的配置文件改变时,需要执行的操作,例如:重新启动服务,重新装载配置文件;

(notify与handers连用模块)

Notify:start httpd

Handlers:

- name:start httpd

Meta:角色定义,可以留空;

Tasks:需要执行的任务;(调用特定模块)

Templates:用来执行安装的模板文件,通常是脚本。

Vars:本次安装所定义的变量。

示例:使用playbook构建LAMP环境。

1.1.筹备。

终止httpd服务并卸载httpd服务。

[root@ansible~]"name=httpdstate=stopped"#ansibleall-mservice-a

[root@ansible*]#ansibleall-myum-a"name=httpdstate=removed"

1.2.工作思路。

安装apache

设置mysql。

设置php。

联系。

设置测试页面。

1.3.建立资料夹。

#mkdir-p/etc/ansible/roles/{httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}

1.4apache的本地安装。

[root@ansibleetc]#yuminstall-yhttpd

1.5apache本地优化。

[root@ansibleetc]#vim/etc/httpd/conf/httpd.conf

ServerName值改为本地值。

当apache启动时,会自动进行dns解析,这需要花费时间,而配置本地地址可以节省大量时间。

1.6在httpd角色中将apache配置文件放在files中。

[root@ansibleetc]#cp/etc/httpd/conf/httpd.conf/etc/ansible/roles/httpd/files/

1.7将mysql配置文件复制到files目录下的mysql角色下。

[root@ansibleetc]#cp/etc/my.cnf/etc/ansible/roles/mysql/files/

1.8预先准备。

建立pre_main.yaml

[root@ansible ~]#cd/etc/ansible/root@ansible~/roles/

[root@ansible roles]#vimpre_mani.yml

- name:cleantheyum。

Shell:yumcleanall/清除缓存的yum源。

- name:cleantheiptables

Shell:iptables-F/关闭iptables。

- name:cleanthefirewalld

shell: systemctl stop firewalld/关闭防火墙。

1.9php测试准备页面。

转到/etc/ansible/roles/httpd/files目录。

[root@ansible ~]#cd/etc/ansible/roles/httpd/files

建立测试页面。

[root@ansiblefiles]#vimindex.php

1.10创建apache角色的分镜头剧本。

[root@ansible~]#vim/etc/ansible/roles/httpd/tasks/main.yaml

名:webserverinstall.…

yum: name=httpdstate=installed/安装apache

- name:providetestpage

copy:src=index.phpdest=/var/www/html/向远程主机进行复制。

- name:deleteapacheconfigfile

Shell:rm-rf/etc/httpd/conf/httpd.conf/RemotehostsProfiles。

- name:provideconfigfile。

src=httpd.confdest=/etc/httpd/conf//将服务器配置文件复制到一个远程主机。

--name:start httpd

service:名称=httpdstate=started/startedService。

默认情况下,src从文件目录找到。

在安装了apache之后,系统默认创建了/var/www/html目录。

1.11创建一个mysql角色的分镜头脚本。

[root@ansible~]#vim/etc/ansible/roles/mysql/tasks/main.yaml

--name:installmysql。

name=mariadb-serverstate=installed

--name:deletetheconfig

shell:rm-rf/etc/my.cnf

--name:copytheconfig

copy:src=my.cnfdest=/etc/

--name:startmysql。

service: name=mariadb state=started

1.12创建php角色的分镜头脚本。

[root@ansible~]#vim/etc/ansible/roles/php/tasks/main.yaml

--name:installphp。

name=phpstate=installed。

--name:installphp-mysql。

name=php-mysqlstate=installed

1.13建立主要镜头的剧本。

[root@ansible ~]#cd/etc/ansible/roles/

[root@ansibleroles]#vimmain.yaml

--name:LAMPbuild。

remote_user:根。

hosts:all。

roles:

-httpd

-mysql

-php

1.14脚本执行。

[root@ansible roles]# ansible-playbook main.yaml

大家都在看

学员故事|小白变大佬,全凭实力!

2024-07-08 浏览次数:0

Java面试八股文指的是什么?Java开发岗位必...

2024-07-08 浏览次数:0

知了堂到底带了什么去川职院,站的位置都没了

2024-07-08 浏览次数:0

安全服务工程师入门难吗?揭秘安全服务工程师的挑战...

2024-07-08 浏览次数:0

成都Java培训出来能拿多少工资

2024-07-08 浏览次数:0

攻防渗透信息收集方法+工具之ip信息收集

2024-07-08 浏览次数:0
最新资讯