流程控制优化

Posted 陈金峰的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了流程控制优化相关的知识,希望对你有一定的参考价值。

1.安装ansible
[root@m01 ~]# yum install -y ansible

2.修改ansible配置文件
[root@m01 ~]# vim /etc/ansible/ansible.cfg
host_key_checking = False             # 把前面的#注释去掉,跳过检查主机指纹
log_path = /var/log/ansible.log       # 把前面的#注释去掉,打开日志文件

3.创建秘钥对

4.发公钥

5.创建ansible_project目录,并在其目录下每个模块创建一个目录
[root@m01 ~]# mkdir -p ansible_project/{nfs,nginx,rsync,php,user,mariadb,sersync,wordpress,mount}

6.创建一个主yml文件
[root@m01 ~]# vim task.yml

7.给每个目录配置不同的配置文件
## 准备
[root@m01 user]# vim ready.yml
- name: Stop Firewalld Server
  service:
    name: firewalld
    state: stopped

- name: Disabled Selinux
  selinux:
    state: disabled

- name: Create www Group
  group:
    name: www
    gid: 666
    state: present
- name: Create www User
  user:
    name: www
    uid: 666
    group: \'666\'
    shell: /sbin/nologin
    create_home: no
    state: present
    
## mariadb
[root@m01 mariadb]# vim create_data_user.yml
- name: create wordpress database
  mysql_db:
    name: wordpress
    state: present
    encoding: utf8
  when: ansible_hostname == \'db01\'

- name: create wordpress user
  mysql_user:
    name: wordpress
    state: present
    priv: "wordpress.*:ALL"
    host: "%"
    passwd: "123"
  when: ansible_hostname == \'db01\'

[root@m01 mariadb]# vim install_mariadb.yml 
- name: install mariadb
  yum:
    name:
      - mariadb-server
      - MySQL-python
    state: present
  when: ansible_hostname == \'db01\'
    
[root@m01 mariadb]# vim start_mariadb.yml 
- name: start mariadb
  service:
    name: mariadb
    state: started
    enabled: yes
  when: ansible_hostname == \'db01\'

## nfs
[root@m01 nfs]# vim nfs_conf.yml
- name: config nfs
  copy:
    content: \'/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)\'
    dest: /etc/exports
  notify: restart nfs
  when: ansible_hostname == "nfs"

[root@m01 nfs]# vim create_data.yml
- name: create data
  file:
    path: /data
    owner: "{{  item.owner }}"
    group: "{{  item.group }}"
    mode: "{{ item.mode }}"
    state: directory
  with_items:
    - {owner: \'www\',group: \'www\',mode: \'0600\'}
  when: ansible_hostname == "nfs"
 
[root@m01 nfs]# vim install_nfs.yml 
- name: install nfs
  yum:
    name: nfs-utils
    state: present
  when: ansible_hostname == "web01" or ansible_hostname == "web02" or ansible_hostname == "nfs" or ansible_hostname == "backup"

[root@m01 nfs]# vim start_nfs.yml 
- name: start nfs
  service:
    name: nfs-server
    state: started
    enabled: yes
  when: ansible_hostname == "nfs" or ansible_hostname == "backup" 
  
## nginx
[root@m01 nginx]# vim push_nginx_conf.yml 
- name: push nginx conf
  copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest}}"
  with_items:
    - {src: "./nginx.conf",dest: "/etc/nginx/nginx.conf"}
    - {src: "./blog.conf",dest: "/etc/nginx/conf.d/blog.conf"}
  notify: reload nginx
  when: ansible_hostname is match \'web*\'

[root@m01 nginx]# vim install_nginx.yml 
- name: Install nginx
  yum:
    name: nginx
    state: present
  when: ansible_hostname is match \'web*\'
  
[root@m01 nginx]# vim start_nginx.yml 
- name: start nginx
  service:
    name: nginx
    state: started
    enabled: yes
  when: ansible_hostname is match \'web*\'
  
## php
[root@m01 php]# vim push_php_conf.yml
- name: copy php conf
  copy:
    src: ./www.conf
    dest: /etc/php-fpm.d/www.conf
  when: ansible_hostname is match \'web*\'

[root@m01 php]# vim install_php.yml 
- name: push php
  unarchive:
    src: "{{ item.src}}"
    dest: "{{ item.dest}}"
  with_items:
    - {src: "./php.tgz",dest: "/tmp"}
  when: ansible_hostname is match \'web*\'
- name: install php
  shell: "rpm -Uvh /tmp/*.rpm"
  ignore_errors: yes
  when: ansible_hostname is match \'web*\'

[root@m01 php]# vim start_php.yml 
- name: start php
  service:
    name: php-fpm
    state: started
    enabled: yes
  when: ansible_hostname is match \'web*\'

## rsync
[root@m01 rsync]# vim install_rsync.yml 
- name: install rsync
  yum:
    name: rsync
    state: present
  when: ansible_hostname == \'backup\'

[root@m01 rsync]# vim push_rsync.yml 
- name: configure rsync server
  copy:
    src: ./rsyncd.conf
    dest: /etc/rsyncd.conf
  notify: restart rsync
  when: ansible_hostname == \'backup\'

- name: create backup
  file:
    path: "{{ item.path }}"
    owner: "{{ item.owner }}"
    group: "{{ item.group }}"
    state: directory
  with_items:
    - {path: "/backup",owner: "www",group: "www"}
  when: ansible_hostname == \'backup\'

- name: copy rsync passwd
  copy:
    content: \'rsync_backup:123\'
    dest: /etc/rsyncd.passwd
    mode: "0600"
  when: ansible_hostname == \'backup\'
  
[root@m01 rsync]# vim start_rsync.yml 
- name: start rsync
  service:
    name: rsyncd
    state: started
    enadled: yes
  when: ansible_hostname == \'backup\'
  
## sersync
[root@m01 sersync]# vim push_sersync_conf.yml
- name: unarchive sersyncd.tar.gz
  unarchive:
    src: ./sersyncd.tar.gz
    dest: /root
  when: ansible_hostname == \'nfs\'

- name: copy service
  copy:
    src: ./sersyncd.service
    dest: /usr/lib/systemd/system/sersyncd.service
  when: ansible_hostname == \'nfs\'

[root@m01 sersync]# vim start_sersync.yml
- name: start sersync
  service:
    name: sersyncd
    state: started
    enabled: yes
  when: ansible_hostname == \'nfs\'
  
## wordpress
[root@m01 wordpress]# vim wordpress.yml 
- name: Create code
  file:
    path: /code
    owner: "{{ item.owner }}"
    group: "{{ item.group }}"
    mode: "{{ item.mode }}"
    state: directory
  with_items:
    - {owner: "www",group: "www",mode: "0755"}
  when: ansible_hostname is match \'web*\'

- name: unarchive wordpress
  unarchive:
    src: ./latest.tar.gz
    dest: /code
    owner: www
    group: www
  when: ansible_hostname is match \'web*\'

- name: mkdir uploads
  file:
    path: /code/wordpress/wp-content/uploads
    state: directory
    owner: www
    group: www
    mode: 0755
  when: ansible_hostname is match \'web*\'

- name: chmod /var/lib/nginx
  file:
    path: /var/lib/nginx
    owner: www
    group: www
    recurse: yes
  when: ansible_hostname is match \'web*\'

## 挂载
[root@m01 mount]# vim mount.yml
- name: mount
  mount:
    path: /code/wordpress/wp-content/uploads
    src: 172.16.1.31:/data
    fstype: nfs
    state: mounted
  when: ansible_hostname is match \'web*\'

8.创建入口文件
[root@m01 ansible_project]# vim task.yml
- hosts: all
  tasks:
    - include_tasks: user/ready.yml
    - include_tasks: mariadb/install_mariadb.yml 
    - include_tasks: mariadb/start_mariadb.yml
    - include_tasks: mariadb/create_data_user.yml
    - include_tasks: nfs/install_nfs.yml 
    - include_tasks: nfs/nfs_conf.yml
    - include_tasks: nfs/create_data.yml
    - include_tasks: nfs/start_nfs.yml
    - include_tasks: nginx/install_nginx.yml 
    - include_tasks: nginx/push_nginx_conf.yml
    - include_tasks: nginx/start_nginx.yml 
    - include_tasks: php/install_php.yml
    - include_tasks: php/push_php_conf.yml
    - include_tasks: php/start_php.yml 
    - include_tasks: rsync/install_rsync.yml 
    - include_tasks: rsync/push_rsync.yml 
    - include_tasks: rsync/start_rsync.yml 
    - include_tasks: sersync/push_sersync_conf.yml
    - include_tasks: sersync/start_sersync.yml
    - include_tasks: wordpress/wordpress.yml 
    - include_tasks: mount/ mount.yml

以上是关于流程控制优化的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

PHP中的流程控制

优化 C# 代码片段、ObservableCollection 和 AddRange

在导航控制器中杀死片段