Ansible playbook编写Apache角色

编写Apache角色;使用源码安装

在files中下载扩展包和安装包

[root@localhost project]# ls roles/httpd/files/
apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.46.tar.gz  pcre-8.40.tar.gz

在taskstasks目录中写入main.yml文件

[root@localhost project]# vim roles/httpd/tasks/main.yml 
--
#安装环境
- name: Install environment
  yum:
    name: "{{ item }}"
    state: present
  loop:
    - make
    - gcc
    - gcc-c++
    - expat-devel
    - wget

#安装APR拓展包
- name: Install apr package
  copy:
    src: ../files/apr-1.6.5.tar.gz
    dest: /root/
  notify:
    - compile apr

#安装apr-util 拓展包
- name: Install apr-util package
  copy:
    src: ../files/apr-util-1.6.1.tar.gz
    dest: /root/
  notify:
    - compile apr-util

#安装PCRE拓展包
- name: Install pcre package
  copy:
    src: ../files/pcre-8.40.tar.gz
    dest: /root/
  notify:
    - complie pcre

#安装httpd软件包
- name: Install Apache package
  copy:
    src: ../files/httpd-2.4.46.tar.gz
    dest: /root/
  notify:
    - Complie Apache
    - Changed configuration files
    - Start Apache
    - set up link

#解压apt、apt-util、pcre、apache压缩包
- name: unzip package
  shell: "{{ item }}"
  loop:
    - tar -zxvf /root/apr-1.6.5.tar.gz
    - tar -zxvf /root/apr-util-1.6.1.tar.gz
    - tar -zxvf /root/pcre-8.40.tar.gz
    - tar -zxvf /root/httpd-2.4.46.tar.gz

#为Apache创建使用用户
- name: Create user for Apache
  user:
    name: "{{ user_name }}"
    state: present
    create_home: no
    system: yes

在handlers目录中写入main.yml文件

[root@localhost project]# vim roles/httpd/handlers/main.yml 
---
#预编译、编译、安装APR
- name: compile apr
  shell: "{{ item }}"
  loop:
    - ./configure --prefix=/usr/local/apr
    - make && make install
  args:
    chdir: "{{ package_apr }}"

#预编译、编译、安装apr-util
- name: compile apr-util
  shell: "{{ item }}"
  loop:
    - ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr
    - make && make install
  args:
    chdir: "{{ package_apr_util }}"

#预编译、编译、安装pcre
- name: complie pcre
  shell: "{{ item }}"
  loop:
    - ./configure --prefix=/usr/local/pcre
    - make && make install
  args:
    chdir: "{{ package_pcre }}"

#预编译、编译、安装Apache
- name: Complie Apache
  shell: "{{ item }}"
  loop:
    - ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre
    - make && make install
  args:
    chdir: "{{ package_httpd }}"

#修改配置文件
- name: Changed configuration files
  shell: sed -i "s/#ServerName www.example.com:80/ServerName localhost:80/" httpd.conf
  args:
    chdir: /usr/local/httpd/conf

#给Apache启动脚本创建软连接到普通用户的环境变量    
- name: set up link
  shell: 'ln -s /usr/local/httpd/bin/apachectl /usr/local/bin/apachectl'

#脚本控制启动httpd
- name: Start Apache
  shell: apachectl start

在vars目录中写变量文件

[root@localhost project]# vim roles/httpd/vars/main.yml 
---
user_name: apache
package_apr: apr-1.6.5
package_apr_util: apr-util-1.6.1
package_pcre: pcre-8.40
package_httpd: httpd-2.4.46

写需要执行的playbook.ymlyml文件

[root@localhost project]# cat playbook.yml 
---
- name: Install Httpd
  hosts: all
  roles:
    - httpd

执行playbook.yml文件

[root@localhost project]# ansible-playbook playbook.yml 

PLAY [Install Httpd] ******************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [client.example.com]

TASK [httpd : Install environment] ****************************************************************************************************************************************
changed: [client.example.com] => (item=make)
changed: [client.example.com] => (item=gcc)
changed: [client.example.com] => (item=gcc-c++)
changed: [client.example.com] => (item=expat-devel)
changed: [client.example.com] => (item=wget)
............

执行完后,在浏览器页面访问

posted @ 2020-09-20 08:00  阮小言  阅读(293)  评论(0编辑  收藏  举报