Ansible 您所在的位置:网站首页 pip卸载ansible Ansible

Ansible

2023-06-25 02:26| 来源: 网络整理| 查看: 265

使用roles安装lnmp

nginx

1、准备四台主机 192.168.142.10 192.168.142.20 192.168.142.30 192.168.142.40 2、10作为ansible管理端 首先ssh连接剩下三台主机 3、vim/etc/ansible/hosts 添加[nginxservers]配置nginx ip,[phpservers]php ip,[mysqlservers]mysql ip 4、cd /etc/ansible  mkdir roles/{nginx,php,mysql} mkdir -p roles/nginx/{tasks,vars,files,templates,handlers,defaults,meta} mkdir -p roles/php/{tasks,vars,files,templates,handlers,defaults,meta} mkdir -p roles/mysql/{tasks,vars,files,templates,handlers,defaults,meta} touch roles/nginx/{tasks,vars,handlers,defaults,meta}/main.yml touch roles/php/{tasks,vars,handlers,defaults,meta}/main.yml touch roles/mysql/{tasks,vars,handlers,defaults,meta}/main.yml 5、准备nginx配置模板文件,需要在管理端下载nginx、php、mysql。 cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.j2 mv default.conf.j2 /etc/ansible/roles/nginx/templates/ 6、在模板文件中修改j2模板文件 listen       {{http_port}}; server_name  {{host_name}}; root   {{root_dir}}; location ~ \.php$ {         root           {{root_dir}};         fastcgi_pass   {{remote_addr}}:{{remote_port}};         fastcgi_index  index.php;         fastcgi_param  SCRIPT_FILENAME  {{root_dir}}$fastcgi_script_name;         include        fastcgi_params;     } 7、在vars文件中准备变量 vim main.yml http_port: 192.168.142.20:80 host_name: www.kgc.com root_dir: /usr/share/nginx/html remote_addr: 192.168.142.30 remote_port: 9000 pkg: nginx service: nginx 8、tasks cd tasks vim init.yml(设置关闭防火墙、selinux) - name: disable selinux   command: '/usr/sbin/setenforce 0'   ignore_errors: true

- name: disable firewalld   systemd: name=firewalld state=stopped enabled=no   ignore_errors: true 这个文件可以复用!! 9、编写main.yml 首先可以进入/etc/ansible/roles/nginx/files vim index.php 再将nginx.repo 复制道files中 cp /etc/yum.repos.d/nginx.repo ./

vim /tasks/main.yml

- include: "init.yml"

- name: copy local yum repo file   copy: src=nginx.repo dest=/etc/yum.repos.d/

- name: install nginx   yum: name=nginx state=latest

- name: copy index.php   copy: src=index.php dest={{root_dir}}

- name: copy template configure file   template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf   notify: restart service

- name: start nginx   systemd: name=nginx state=started enabled=yes

vim /handlers/main.yml - name: restart service   systemd: name=nginx state=restarted

10、创建playbook yml文件 cd /etc/ansible mkdir playbook

vim lnmproles.yaml

- name: nginx play   hosts: nginxservers   remote_user: root   roles:   - nginx

ansible-playbook lnmproles.yaml 启动剧本

准备mysql

先在本地安装mysql yum remove mariadb* -y cd /etc/yum.repos.d

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm yum -y install mysql57-community-release-el7-10.noarch.rpm sed -i 's/gpgcheck=1/gpgcheck=0/' mysql-community.repo yum -y install mysql-community-server 或 wget https://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm rpm -ivh mysql57-community-release-el7-11.noarch.rpm sed -i 's/gpgcheck=1/gpgcheck=0/' mysql-community.repo yum -y install mysql-server

systemctl start mysqld.service systemctl enable mysqld.service

grep "password" /var/log/mysqld.log            #在日志文件中找出root用户的初始密码 2021-07-31T07:55:00.366359Z 1 [Note] A temporary password is generated for root@localhost: ga7%{item}}   with_items:    - php72w    - php72w-cli    - php72w-common    - php72w-devel    - php72w-embedded    - php72w-gd    - php72w-mbstring    - php72w-pdo    - php72w-xml    - php72w-fpm    - php72w-mysqlnd    - php72w-opcache    - php72w-redis

- name: create php user   user: name={{user_name}}

- name: copy php configure file   copy: src=php.ini dest=/etc/php.ini

- name: modify www.conf   replace: path=/etc/php-fpm.d/www.conf regexp="apache" replace="php"

- name: modify listen address   replace: path=/etc/php-fpm.d/www.conf regexp="127.0.0.1:9000" replace={{http_port}}

- name: modify allowed_clients   replace: path=/etc/php-fpm.d/www.conf regexp="127.0.0.1" replace={{remote_addr}}

- name: create root dir   file: state=directory path={{root_dir}}

- name: copy index.php   copy: src=index.php dest={{root_dir}}

- name: start php   systemd: name=php-fpm state=started enabled=yes

cp /etc/ansible/roles/nginx/tasks/init.yml /etc/ansible/roles/php/tasks

cp /etc/ansible/roles/nginx/files/index.php /etc/ansible/roles/php/files/

cd /php/vars

vim main.yml

user_name: php http_port: 192.168.142.30:9000 remote_addr: 192.168.142.20 root_dir: /usr/share/nginx/html

cd /etc/ansible/playbook

vim lnmproles.yaml

- name: nginx play   hosts: nginxservers   remote_user: root   roles:   - nginx

- name: mysql play   hosts: mysqlservers   remote_user: root   roles:   - mysql

- name: php play   hosts: phpservers   remote_user: root   roles:   - php

 安装成功

验证

浏览器访问:http://192.168.142.20/index.php



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有