ansible roles练习

1.配置主机清单

[root@m01 roles]# vim /etc/ansible/hosts 
[lb_server]
lb01 ansible_ssh_pass='1'
lb02 ansible_ssh_pass='1'

[web_group]
web01 ansible_ssh_pass='1'
web02 ansible_ssh_pass='1'

[nfs_server]
nfs ansible_ssh_pass='1'

[rsync_server]
backup ansible_ssh_pass='1'

[db_server]
db01 ansible_ssh_pass='1'

[nginx:children]
web_group
lb_server

2.配置hosts

[root@m01 roles]# vim /etc/hosts
10.0.0.4 lb01
10.0.0.5 lb02
10.0.0.7 web01
10.0.0.8 web02
10.0.0.31 nfs
10.0.0.41 backup
10.0.0.51 db01

3.优化部分

[root@m01 roles]# cat base/tasks/main.yml 
- hosts: all
  tasks:
    - name: Stop Firewalld
      systemd:
        name: firewalld
        state: stopped
        enabled: no

    - name: Stop Selinux
      selinux:
        state: disabled

    - name: Create www Group
      group:
        name: www
        gid: 666
        state: present
      when: ansible_fqdn != "db01"

    - name: Create www User
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: false
        state: present
      when: ansible_fqdn != "db01"

4.nginx部分

1)准备包和配置文件

[root@m01 roles]# cd nginx/files/
[root@m01 files]# ll
total 772
-rw-r--r-- 1 root root 784272 Dec 10 09:13 nginx-1.16.1-1.el7.ngx.x86_64.rpm
-rw-r--r-- 1 root root    641 Dec 24 17:16 nginx.conf

2)安装nginx

[root@m01 roles]# vim nginx/tasks/main.yml 
- hosts: nginx
  tasks:
    - name: Push nginx rpm
      copy:        src: nginx-1.16.1-1.el7.ngx.x86_64.rpm
        dest: /tmp/

    - name: Install Nginx Server
      yum:
        name: /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm
        state: present

    - name: Config Nginx Server
      copy:
        src: nginx.conf
        dest: /etc/nginx/
      notify: restart_nginx

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

3)配置触发器

[root@m01 roles]# vim nginx/handlers/main.yml 
- name: restart_nginx
  systemd:
    name: nginx
    state: restarted

5.php部分

1)准备配置文件

[root@m01 roles]# cd php/files/
[root@m01 files]# ll
total 19508
-rw-r--r-- 1 root root    62646 Dec 21 11:04 php.ini
-rw-r--r-- 1 root root 19889622 Nov 22 15:52 php.tar.gz
-rw-r--r-- 1 root root    17962 Dec 22 15:10 www.conf

2)编写安装PHP的剧本

[root@m01 roles]# vim php/tasks/main.yml
- hosts: web_group
  tasks:
    - name: Tar php Package
      unarchive:
        src: php.tar.gz
        dest: /tmp/

    - name: Check php Install Status
      shell: "rpm -qa | grep php | wc -l"
      register: get_php_install_status
      changed_when: false

    - name: Install php Server
      shell: "yum localinstall -y /tmp/*.rpm"
      when: get_php_install_status.stdout_lines == 0

    - name: Config php Server
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
      with_items:
        - { src: "php.ini", dest: "/etc" }
        - { src: "www.conf", dest: "/etc/php-fpm.d/" }
      notify: restart_php

    - name: Start php Server
      systemd:
        name: php-fpm
        state: started

3)配置触发器

[root@m01 roles]# vim php/handlers/main.yml 
- name: restart_php
  systemd:
    name: php-fpm
    state: restarted

6.数据库部分

联系管理员微信tutu19192010,注册账号

上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 沪ICP备2022009235号 沪公网安备31011802005110号 青浦区尚茂路798弄 联系方式-13697081366