目录
Toggle自动部署nfs(服务端部署+客户端挂载)
13.nfs_srv_cli_v2.yml
---
- hosts: nfs
tasks:
- name: 1. yum安装nfs-utils,rpcbind
yum:
name:
- nfs-utils
- rpcbind
state: present
tags: "01"
- name: 2. template导入配置文件(文件中有变量)
template:
src: /server/scripts/ansible/exports.j2
dest: /etc/exports
backup: yes
tags: "02"
notify:
- 重启服务
#- name: 2. lineinfile修改配置文件
#lineinfile:
# path: /etc/exports
# regexp: "^/nfs/ans/"
# line: |
# ansible-nfs
# /nfs/ans/ 172.16.1.0/24 (rw,all_squash,anonuid=2999,anongid=2999)
- name: 3. 先创建www group
group:
name: "{{ nfs_user }}"
gid: "{{ nfs_user_uid }}"
state: present
tags: "03"
- name: 3.5 将user添加www,uid 1999
user:
name: "{{ nfs_user }}"
uid: "{{ nfs_user_uid }}"
group: "{{ nfs_user }}"
create_home: no
shell: /sbin/nologin
comment: "Ansible created service account"
state: present
tags: "04"
- name: 4. file创建共享目录并改所有者
file:
path: "{{ nfs_dir }}"
owner: "{{ nfs_user }}"
group: "{{ nfs_user }}"
state: directory
tags: "05"
- name: 5. systemd启动服务rpcbind,nfs(注意顺序,一次只能管一个)
systemd:
name: "{{ item }}"
enabled: yes
state: started
loop:
- rpcbind
- nfs
tags: "06"
handlers: #设置踩雷后操作.
- name: 重启服务
systemd:
name: nfs
state: reloaded
tags: "07"
- hosts: web
tasks:
- name: 1. 安装nfs-utils
yum:
name: nfs-utils
state: present
tags: "01"
- name: 2. 创建挂载点
file:
path: "{{ web_upload }}"
state: directory
tags: "09"
- name: 3. 先创建www group
group:
name: "{{ web_user }}"
gid: "{{ web_user_uid }}"
state: present
tags: "10"
- name: 3.5 将user添加www,uid 1999
user:
name: "{{ web_user }}"
uid: "{{ web_user_uid }}"
group: "{{ web_user }}"
create_home: no
shell: /sbin/nologin
comment: "Ansible created service account"
state: present
tags: "11"
- name: 3.挂载与永久挂载
mount:
src: "{{ nfs_server_ip }}:{{ nfs_dir }}"
path: "{{ web_upload }}"
fstype: nfs
state: mounted
tags:
- "01"
- "a1"
LNMP批量加机器
add_machine.yml
---
- hosts: web1
include_tasks:
- web_src_old/tasks/total_web1.yml
- hosts: web2
roles:
- web_dest_new
从web1导出站点目录:
total_web1.yml
---
- hosts: web01
tasks:
- name: 1.传输站点目录代码
shell: scp -rp /app/code/blog/* root@web02:/app/code/blog/ &>/dev/null
增加n台web服务器
total_add_web2.yml
---
- name: 针对 web2 执行任务
hosts: web2
vars:
packages:
- nginx
- php
- php-bcmath
- php-cli
- php-common
- php-devel
- php-embedded
- php-fpm
- php-gd
- php-intl
- php-mbstring
- php-mysqlnd
- php-opcache
- php-pdo
- php-process
- php-xml
- php-json
tasks:
- name: 当前执行剧本的机器
debug:
msg: "这是在 {{ inventory_hostname }} 机器上执行的任务"
- name: 1. 安装nginx,php
yum:
name: "{{ packages }}"
state: present
# yum模块本身就支持直接传递一个包列表,这样可以避免循环,并且只需要一个任务即可安装所有包
# (推荐,因为这样只需要一次yum事务):
# 这里使用列表方式安装的好处是,yum会一次性安装所有包,只需要一个事务,比循环安装每个包更高效。
- name: 2.1创建www用户组
group:
name: www
gid: 1999
state: present
- name: 2.2创建www用户
user:
name: www
group: www
create_home: no
shell: /sbin/nologin
state: present
- name: 3.修改nginx站点目录配置文件/conf.d/blog.linuxjk.cn.conf #(直接copy,与web1保持一致即可)
copy:
src: files/blog.linuxjk.cn.conf
dest: /etc/nginx/conf.d/blog.linuxjk.cn.conf
- name: 4.1修改nginx用户为www,与nfs统一
lineinfile:
path: /etc/nginx/nginx.conf
regexp: '^user'
line: "user www;"
- name: 5.1检查nginx配置是否正确
shell: nginx -t &>/dev/null
register: check_nginx
- name: 4.2 修改php用户为www,与nfs统一,修改php监听socket文件的所属主所属组为www,权限0660
lineinfile:
path: /etc/php-fpm.d/www.conf
# 使用 {{ item.olduser }} 来正确引用变量
regexp: "{{ item.olduser }}"
line: "{{ item.newuser }}"
# 确保修改后配置文件语法正确
backrefs: yes
loop:
- { olduser: '^;?\s*user\s*=', newuser: 'user = www' }
- { olduser: '^;?\s*group\s*=', newuser: 'group = www' }
- { olduser: '^;?\s*listen.owner\s*=', newuser: 'listen.owner = www' }
- { olduser: '^;?\s*listen.group\s*=', newuser: 'listen.group = www' }
- { olduser: '^;?\s*listen.mode\s*=', newuser: 'listen.mode = 0660' }
- name: 4.3 修改php.ini(上传文件大小,默认2M)
lineinfile:
path: /etc/php.ini
regexp: "{{ item.oldsize }}"
line: "{{ item.newsize }}"
backrefs: yes
loop:
- { oldsize: '^;?\s*upload_max_filesize\s*=.*', newsize: 'upload_max_filesize = 500M'}
- { oldsize: '^;?\s*post_max_size\s*=.*', newsize: 'post_max_size = 512M'}
- { oldsize: '^;?\s*memory_limit\s*=.*', newsize: 'memory_limit = 256M'}
- { oldsize: '^;?\s*max_execution_time\s*=.*', newsize: 'max_execution_time = 300'}
- name: 5.2检查php配置是否正确
shell: php-fpm -t &>/dev/null
register: check_php
- name: 6.1设置开机自启动并启动nginx服务
systemd:
name: nginx
enabled: yes
state: started
when: check_nginx.rc == 0
- name: 提示nginx配置错误
debug:
msg: |
nginx配置文件有误,请检查配置
when: check_nginx.rc != 0
- name: 6.2设置开机自启动并启动php服务
systemd:
name: php-fpm
enabled: yes
state: started
when: check_php.rc == 0
- name: 提示php配置错误
debug:
msg: |
php配置文件有误,请检查配置
when: check_php.rc != 0
- name: 8.创建临时目录存放网页目录文件
file:
path: /tmp/test/
state: directory
- name: 8.将upload目录下的内容转移出去
shell: mv /app/code/blog/wp-content/uploads/* /tmp/test &>/dev/null
ignore_errors: yes # 添加错误忽略
- name: 9. web2 挂载 nfs并设置永久挂载
mount:
src: 172.16.1.31:/ans_www/web1
path: /app/code/blog/wp-content/uploads
fstype: nfs
state: mounted
register: mount_nfs
- name: 10. 显示挂载结果(成功)
debug:
msg: mount is successful
when: not mount_nfs.failed
- name: 10. 显示挂载结果(失败)
debug:
msg: mount is failed
when: mount_nfs.failed
- name: 11. 调整站点目录,nginx工作目录权限
file:
path: "{{ item }}"
owner: www
group: www
loop:
- /app/code/blog/
- /var/log/nginx/
- /var/lib/nginx/