Ansible 使用04

1、利用 Playbook 实现批量编译安装部署 httpd-2.4

# 准备httpd service模板文件
[root@ansible ansible]# vim httpd.service.j2

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart={{ install_dir }}/bin/apachectl start
ExecReload={{ install_dir }}/bin/apachectl graceful
ExecStop={{ install_dir }}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@ansible ansible]#cat install_httpd.yml
---

- hosts: websrvs
  remote_user: root
  vars:
    download_dir: /usr/local/src
    install_dir: /apps/httpd
    httpd_version: httpd-2.4.54
    apr_version: apr-1.7.0
    apr_util_version: apr-util-1.6.1
    httpd_url: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd
    apr_url: https://mirrors.tuna.tsinghua.edu.cn/apache/apr

  tasks:
    - name: install packages
      yum:
        name:
          - gcc
          - make
          - pcre-devel
          - openssl-devel
          - expat-devel
          - bzip2
        state: installed
    - name: download httpd file
      unarchive:
        src: "{{ httpd_url }}/{{ httpd_version }}.tar.bz2"
        dest: {{ download_dir }}
        owner: root
        remote_src: yes
    - name: download apr file
      unarchive:
        src: "{{ apr_url }}/{{ apr_version }}.tar.bz2"
        dest: {{ download_dir }}
        owner: root
        remote_src: yes
    - name: download apr_util file
      unarchive:
        src: "{{ apr_url }}/{{ apr_util_version }}.tar.bz2"
        dest: {{ 
download_dir }}
        owner: root
        remote_src: yes
    - name: prepare apr dir
      shell: chdir={{ download_dir }} mv {{ apr_version }} {{ download_dir }}/{{ httpd_version }}/srclib/apr
    - name: prepare apr_util dir
      shell: chdir={{ download_dir }} mv {{ apr_util_version }} {{ download_dir   }}/{{ httpd_version }}/srclib/apr-util
    - name: build httpd
      shell: chdir={{ download_dir }}/{{ httpd_version }} ./configure --prefix={{ install_dir }} --enable-so  --enable-ssl  --enable-cgi  --enable-rewrite  --
with-zlib  --with-pcre --with-included-apr  --enable-modules=most  --enable-mpms-shared=all  --with-mpm=prefork &&  make -j {{ ansible_processor_vcpus }} && make install
    - name: create group
      group:
        name: apache
        gid: 80
        system: yes
    - name: create user
      user:
        name: apache
        uid: 80
        group: apache
        shell: /sbin/nologin
        system: yes
        create_home: no
        home: {{ install_dir }}/conf/httpd
    - name: set httpd user
      lineinfile:
        path: {{ install_dir }}/conf/httpd.conf
        regexp: '^User'
        line: 'User apache'
    - name: set httpd group
      lineinfile:
        path: {{ install_dir }}/conf/httpd.conf
        regexp: '^Group'
        line: 'Group apache'
    - name: set variable PATH
      shell: echo PATH={{ install_dir }}/bin:$PATH >> /etc/profile.d/httpd.sh
    - name: prepare service file
      template:
        src: ./httpd.service.j2
        dest: /usr/lib/systemd/system/httpd.service
    - name: start service
      service:
        name: httpd
        state: started
        enabled: yes

2、利用 Playbook 实现 NFS 服务端

# 准备模板文件
[root@ansible ansible]# vim exports.j2

/nfs_test 192.168.0.0/16(rw,all_squash,anonuid=666,anongid=666)
[root@ansible ansible]# vim install_nfs_server.yml
---

- hosts: webservers
  tasks:
    - name: 1.Installed NFS Server
      yum:
        name: nfs-utils
        state: present
    - name: 2.Configure NFS Server
      copy:
        src: ./exports.j2
        dest: /etc/exports
      notify: Restart NFS Server
    - name: 3.Init Group
      group:
        name: www
        gid: 666
    - name: 4.Init User
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: no
    - name: 5.Init Create Directory
      file:
        path: /nfs_test
        state: directory
        owner: www
        group: www
        mode: "0755"
    - name: 6.Started NFS Server
      systemd:
        name: nfs
        state: started
        enabled: yes
  handlers:
    - name: Restart NFS Server
      systemd:
        name: nfs
        state: restarted

3、利用 Playbook 实现 Redis 服务端

---

- hosts: dbservers
  tasks:
    - name: Installed Redis Server
      yum:
        name: redis
        state: present
    - name: Configure Redis Server
      copy:
        src: ./files/redis.conf.j2
        dest: /etc/redis.conf
        owner: redis
        group: root
        mode: 0640
      notify: Restart Redis Server
    - name: Systemd Redis Server
      systemd:
        name: redis
        state: started
        enabled: yes
  handlers:
    - name: Restart Redis Server
      systemd:
        name: redis
        state: restarted

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享