ansible 3 playbook

Playbook实现Rsync备份
1)服务器准备
10.0.0.41  BACKUP服务器
10.0.0.7   客户端
10.0.0.8   客户端

2)10.0.0.41 安装rsync服务
    [root@m01 ~]# mkdir ansible
    [root@m01 ~]# cd ansible

  主机清单:
  [root@m01 ansible]# cat /etc/ansible/hosts
    10.0.0.7
    10.0.0.8

    [webservers]
    web01 ansible_ssh_host=10.0.0.7 

    [dbservers]
    web02 ansible_ssh_host=10.0.0.8

    [lnmp:children]
    webservers
    dbservers

    [nfs]
    172.16.1.31

    [rsync]
    172.16.1.41

   推送公钥做免密钥
   [root@m01 ~]# ssh-copy-id -i .ssh/id_rsa.pub 172.16.1.41


3)配置rsync服务

4)根据配置创建必要的数据

5)启动rsync服务

6)客户端测试
-------------------------------------
[root@m01 ansible]# cat rsync.yml 
- hosts: rsync
  tasks:
    - name: Install Rsync Server
      yum:
        name: rsync
        state: present

    - name: Configure Rsync Server
      copy:
        src: rsyncd.conf
        dest: /etc/rsyncd.conf

    - name: Create Group www
      group: 
        name: www
        gid: 666

    - name: Create www User
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: no

    - name: copy passwd file
      copy:
        content: rsync_backup:123456
        dest: /etc/rsync.passwd
        mode: 0600

    - name: Create backup Dir
      file:
        path: /backup
        state: directory
        owner: www
        group: www

    - name: Start Rsync Server
      systemd:
        name: rsyncd
        state: started
        enabled: yes
- hosts: lnmp
  tasks:
    - name: Create Script Dir
        file:
        path: /server/scripts
        state: directory
    
    - name: Configure Cron
        copy:
        src: backup.sh
        dest: /server/scripts/
    
    - name: Configure Crond
        cron:
        name: "backup passwd"
        minute: "00"
        hour: "00"
        job: "sh /server/scripts/backup.sh"
---------------------------------

部署Nginx PHP MySQL NFS 框架
基于框架搭建业务wordpress 知乎 phpshe

安装多个软件:
[root@m01 ~]# cat yum.yml 
- hosts: web01
  tasks:
    - name: Install sl wget
      yum:
        name: "{{ test }}"
        state: absent
      vars:
        test:
          - sl
          - wget


部署NFS服务
[root@m01 ansible]# cat nfs.yml 
- hosts: nfs
  tasks:
    - name: Install NFS Server
      yum:
        name: nfs-utils
        state: present


    - name: Configure NFS Server
      copy:
        content: /data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
        dest: /etc/exports

    - name: Create Group www
      group:
        name: www
        gid: 666
    - name: Create user www
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: no
    - name: Create /data Dir
      file:
        path: /data
        state: directory
        owner: www
        group: www
    - name: Start NFS Server
      systemd:
        name: nfs
        state: started
        enabled: yes


部署Nginx和PHP服务  (执行过程中需要的配置文件提前准备)
[root@m01 ansible]# cat nginx.yml 
- hosts: lnmp
  tasks:
    - name: Configure YUM Ripository
      yum_repository:
        name: nginx
        description: Nginx YUM REPO
        baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
        enabled: yes
        gpgcheck: no

    - name: Install Nginx Server
      yum:
        name: nginx
        state: present

    - name: Configure Nginx Server
      copy:
        src: nginx.conf
        dest: /etc/nginx/nginx.conf

    - name: Create Group www
      group: 
        name: www
        gid: 666

    - name: Create www User
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: no

    - name: Start Nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes

    - name: Unarchive PHP.tar.gz
      unarchive:
        src: ./php.tar.gz
        dest: /root/

    - name: Install PHP rpm
      yum:
        name: "{{ php_rpm }}"
        state: present
      vars:
        php_rpm:
          - libmcrypt-2.5.8-13.el7.x86_64.rpm
          - libmemcached-1.0.16-5.el7.x86_64.rpm
          - libX11-1.6.7-4.el7_9.x86_64.rpm
          - libX11-common-1.6.7-4.el7_9.noarch.rpm
          - libXau-1.0.8-2.1.el7.x86_64.rpm
          - libxcb-1.13-1.el7.x86_64.rpm
          - libXpm-3.5.12-1.el7.x86_64.rpm
          - mod_php71w-7.1.33-1.w7.x86_64.rpm
          - pcre-devel-8.32-17.el7.x86_64.rpm
          - php71w-cli-7.1.33-1.w7.x86_64.rpm
          - php71w-common-7.1.33-1.w7.x86_64.rpm
          - php71w-devel-7.1.33-1.w7.x86_64.rpm
          - php71w-embedded-7.1.33-1.w7.x86_64.rpm
          - php71w-fpm-7.1.33-1.w7.x86_64.rpm
          - php71w-gd-7.1.33-1.w7.x86_64.rpm
          - php71w-mbstring-7.1.33-1.w7.x86_64.rpm
          - php71w-mcrypt-7.1.33-1.w7.x86_64.rpm
          - php71w-mysqlnd-7.1.33-1.w7.x86_64.rpm
          - php71w-opcache-7.1.33-1.w7.x86_64.rpm
          - php71w-pdo-7.1.33-1.w7.x86_64.rpm
          - php71w-pear-1.10.4-1.w7.noarch.rpm
          - php71w-pecl-igbinary-2.0.5-1.w7.x86_64.rpm
          - php71w-pecl-memcached-3.0.4-1.w7.x86_64.rpm
          - php71w-pecl-mongodb-1.5.3-1.w7.x86_64.rpm
          - php71w-pecl-redis-3.1.6-1.w7.x86_64.rpm
          - php71w-process-7.1.33-1.w7.x86_64.rpm
          - php71w-xml-7.1.33-1.w7.x86_64.rpm
    - name: Configure PHP Server
      copy:
        src: www.conf
        dest: /etc/php-fpm.d/www.conf

    - name: Start PHP Server
      systemd:
        name: php-fpm
        state: started
        enabled: yes


[root@db01 ~]# yum -y install MySQL-python
mysql服务部署:
[root@m01 ansible]# cat mysql.yml 
- hosts: db
  tasks:
    - name: Install Mariadb Server
      yum:
        name: mariadb-server
        state: present

    - name: Start MariaDB Server
      systemd:
        name: mariadb
        state: started
        enabled: yes


部署wordpress业务
[root@m01 ansible]# cat wordpress.yml 
- hosts: lnmp
  tasks:
    - name: Remove Nginx Default.conf
      file:
        path: /etc/nginx/conf.d/default.conf
        state: absent

    - name: Confgire wordpress file
      copy:
        src: wordpress.conf
        dest: /etc/nginx/conf.d/wordpress.conf

    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted


    - name: Create /code/ Dir
      file:
        path: /code
        state: directory

    - name: Wget Wordpress Code
      get_url:
        url: https://cn.wordpress.org/latest-zh_CN.tar.gz
        dest: /code/

    - name: unarchive wordpress.tar.gz
      unarchive:
        src: /code/wordpress-6.0-zh_CN.tar.gz
        dest: /code/
        remote_src: yes
        creates: /code/wordpress
        owner: www
        group: www

- hosts: db
  tasks:
    - name: Create Dabases wordpress
      mysql_db:
        login_user: root
        login_port: 3306
        login_host: localhost
        name: wordpress
        state: present
    - name: create remote user
      mysql_user:
        login_user: root
        login_port: 3306
        login_host: localhost
        name: lpx
        password: 1
        priv: '*.*:ALL'
        host: '%'
        state: present

你可能感兴趣的:(服务器,运维,ssh)