天天看點

ansible-playbook批量編譯部署httpd服務

完整代碼:

https://github.com/ledrsnet/my-ansible-example/tree/main/httpd_simple

[[email protected] httpd_simple]# tree
.
├── ansible.cfg
├── hosts
├── install_httpd.yml
└── roles
    └── common
        ├── defaults
        │   └── main.yml
        ├── files
        │   └── httpd.service
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   ├── compile.yml
        │   ├── main.yml
        │   ├── postcompile.yml
        │   └── precompile.yml
        ├── templates
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

10 directories, 15 files
           

precompile.yml

- name: yum install packages
  yum: name="gcc,make,pcre-devel,openssl-devel,expat-devel,wget,bzip2"
  when: ansible_distribution == "CentOS"
- name: apt install packages
  apt: name="gcc,make,libapr1-dev,libaprutil1-dev,libpcre3,libpcre3-dev,libssl-dev,wget"
  when: ansible_distribution != "CentOS"
- name: get packages and decompress
  unarchive: src={{ item }} dest={{ pk_dir }} copy=no
  with_items: "{{ packages }}"
- name: register apr
  stat: path={{ pk_dir }}{{ filenames.apr }}
  register: apr_result
- name: register apr_util_result
  stat: path={{ pk_dir }}{{ filenames.apr_util }}
  register: apr_util_result
- name: mv apr
  shell: mv {{ pk_dir }}{{ filenames.apr }} {{ pk_dir }}{{ filenames.httpd }}/srclib/apr
  when: apr_result.stat.exists
- name: mv apr_util
  shell:  mv {{ pk_dir }}{{ filenames.apr_util }} {{ pk_dir }}{{ filenames.httpd }}/srclib/apr-util
  when: apr_util_result.stat.exists
           

compile.yml

- name: generate httpd makefile
  shell:
    chdir: "{{ pk_dir }}{{ filenames.httpd }}"
    cmd: ./configure --prefix={{ install_path }} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-sh
ared=all --with-mpm=prefork
- name: compile httpd
  make:
    chdir: "{{ pk_dir }}{{ filenames.httpd }}"
    target: install
    file: "{{ pk_dir }}{{ filenames.httpd }}/Makefile"
           

postcompile.yml

- name: create apache user
  user: name=apache system=yes create_home=no shell=/sbin/nologin
- name: modify config user
  lineinfile:
    path: "{{ install_path }}/conf/httpd.conf"
    regexp: '^User.*'
    line: 'User apache'
- name: modify config group
  lineinfile:
    path: "{{ install_path }}/conf/httpd.conf"
    regexp: '^Group.*'
    line: 'Group apache'
- name: config PATH Variable
  shell: echo 'PATH="{{ install_path }}/bin:$PATH"' > /etc/profile.d/httpd.sh
- name: copy service file
  copy: src=httpd.service dest=/lib/systemd/system/httpd.service
- name: reload service daemon
  shell: systemctl daemon-reload
- name: start Service
  service: name=httpd.service state=started enabled=yes                                                 
           

install_httpd.yml

---
- hosts: webservers

  roles:
    - common