ansible的常用模块 优化策略和角色控制部署wordpress剧本(LNMP架构)

1、ansible的常用模块

ping模块,用来测试主机的连通性

command模块,直接在远程主机上执行命令,并将结果返回本主机

shell模块,可以在远程主机上调用shell解释器运行命令,支持shell的各种功能

copy模块,可以将文件复制到远程机器,同时支持给定内容生成文件和修改权限等

file模块,主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等

fetch模块,从远程某主机获取(复制)文件到本地,必须是文件

cron模块,管理计划任务的

yum模块,用于软件的安装卸载

service模块,管理服务,开启、关闭、重启

user模块,管理用户账号

group模块,添加或删除组

2、ansible剧本格式

---
- hosts: web   #(指示使用哪个主机或主机组来运行下面的 tasks )
  remote_user: root  #(指定远端主机中的哪个用户来登录远端系统)
  vars:    #定义变量并赋值
    db_name: wordpress
    db_password: Leyi@123456
  tasks:    #(指定远端主机将要执行的一系列动作)
    - name: 1.复制yum
      copy: src=/root/yum-server.sh dest=/root/
      notify: copy_yum   #(执行完task后会去执行handlers的copy_yum)
      tags: copyyum  #(标签)
  handlers:
    - name: copy_yum
      copy: src=/root/yum-server.sh dest=/root/

3、写过什么剧本

LNMP架构部署wordpress

#用角色控制(roles)来写wordpress的发布剧本
#首先创建roles目录
[root@ansible ~]# mkdir /root/roles
创建各个角色板块
[root@ansible roles]# ansible-galaxy init nginx
[root@ansible roles]# ansible-galaxy init mysql
[root@ansible roles]# ansible-galaxy init php
[root@ansible roles]# ansible-galaxy init wordpress
#开始写剧本  
#首先是nginx
[root@ansible roles]# cat /root/roles/nginx/tasks/main.yml
---
# tasks file for nginx
   - name: install_yum
     shell: curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
   - name: install_nginx
     yum: name=nginx state=present
   - name: copy_configure
     template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
   - name: start_nginx
     service: name=nginx state=started
   - name: restart_nginx
     service: name=nginx state=restarted
 
[root@ansible roles]# cat /root/roles/nginx/templates/main.yml  #在这个模板文件里定义了两个变量
user {{ nginx_user }};
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       {{ nginx_port }};
        listen       [::]:80;
        server_name  localhost;
        root         /usr/share/nginx/html/wordpress/;
        index index.html index.php;
        location ~ \.php$
        {
		include fastcgi_params;
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php; 
		fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/wordpress$fastcgi_script_name;

        }
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}
  
[root@ansible roles]# cat /root/roles/nginx/vars/main.yml #定义的变量在这个文件里进行赋值
---
# vars file for nginx
nginx_port: 89
nginx_user: nginx

#nginx就部署完成了,下面是php
[root@ansible roles]# cat /root/roles/php/task/main.yml
---
# tasks file for php  
#   - name: install_yum    #因为前面nginx已经装过镜像了,这里就不装了
#     shell: curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

   - name: install_php_rpm #(第一次执行成功后,第二次在执行时要注释掉,不然会报错)
     shell: yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm

   - name: install_php_packages  #(第一次执行成功后,第二次在执行时要注释掉,不然会报错)
     shell: yum install php80-php-xsl php80-php php80-php-cli php80-php-devel php80-php-gd php80-php-pdo php80-php-mysql php80-php-fpm -y

   - name: start php
     service: name=php80-php-fpm state=started

   - name: restart php
     service: name=php80-php-fpm state=restarted

然后是mariadb
[root@ansible roles]# cat /root/roles/mysql/tasks/main.yml
---
# tasks file for mysql
#   - name: install_yum
#     shell: curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

  - name: install_mysql
    shell: yum install -y mariadb-server mariadb

  - name: start_mysql
    service: name=mariadb state=started

#  - name: change_passwd
#    shell: mysqladmin -p"`awk '/temporary password/{p=$NF}END{print p}' /var/log/mysqld.log`" password 'Qianfeng@123'

  - name: create_db
    shell: mysql -e "create database wordpress;"

  - name: grant_user
    shell: mysql -e "grant all on wordpress.* to 'wordpress'@'%' identified by 'Qianfeng@123';"
 
  #最后将wordpress放到nginx的网站根目录去
 [root@ansible roles]# cat /root/roles/wordpress/tasks/main.yml 
 ---
# tasks file for wordpress
   - name: 传包
     unarchive: src=wordpress-6.4.1-zh_CN.tar.gz dest=/usr/share/nginx/html/ 
 #用unarchive的话要将wordpress的压缩包放到/root/roles/wordpress/file/路径下
  
[root@ansible roles]# cat /root/roles/roles.yml  
  - hosts: db
  remote_user: root
  roles: 
    - nginx
    - php
    - mysql
    - wordpress 
    
#写完后先检测一下有没有语法错误,然后看一下执行的主机是谁,再查看一下执行的任务是否正确
[root@ansible roles]# ansible-playbook roles.yml --syntax-check #检测语法
[root@ansible roles]# ansible-playbook roles.yml --list-hosts #看主机
[root@ansible roles]# ansible-playbook roles.yml --list-task #看任务

 网站部署时wp-config.php复制放到网站发布路径下就ok啦。

4、ansible怎么优化

1)关闭ssh密钥检测

2)openssh连接优化

3)ssh pipelining加速ansible

4)facts缓存优化

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值