ansible 使用playbook編譯安裝nginx

2020-10-25 07:01:02

ansible 使用playbook編譯安裝nginx

思路:
安裝nginx的編譯環境
下載檔案、解壓
編譯安裝
playbook:
使用yum、user、get_url、file、unarchive、chmmand這些模組

yum模組(官方):安裝多個軟體包

  • name: ensure a list of packages installed
    yum:
    name: 「{{ packages }}」
    vars:
    packages:
    - httpd
    - httpd-tools
	user模組(官方):建立johnd、指定johnd的uid、並指定組為admin
  • name: Add the user ‘johnd’ with a specific uid and a primary group of ‘admin’
    user:
    name: johnd
    uid: 1040
    group: admin
	get_url模組(官方):下載並指定目錄
  • name: Download foo.conf
    get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    mode: ‘0440’
	file模組(官方):建立目錄可指定許可權
  • name: Create a directory if it does not exist
    file:
    path: /etc/some_directory
    state: directory
    mode: ‘0755’
	unarchive模組(官方):解壓檔案並指定目錄
  • name: Extract foo.tgz into /var/lib/foo
    unarchive:
    src: foo.tgz
    dest: /var/lib/foo
		chmmand模組:先執行chdir(切換目錄)、再執行/usr/bin…………
  • name:
    command: /usr/bin/make_database.sh db_user db_name
    args:
    chdir: somedir/
---
#安裝編譯依賴環境
- name: Install and compile dependent environment
  hosts: all
  tasks:
    - name: install yum
      yum:
        name: "{{ packages }}"
      vars:
        packages:
        - gcc
        - gcc-c++
        - autoconf
        - automake
        - zlib
        - zlib-devel
        - openssl
        - openssl-devel
        - pcre

#建立編譯安裝的使用者
    - name: Create user
      user:
        name: nginx

#下載nginx原始碼包
    - name: Download the nginx source package
      get_url:
        url: https://nginx.org/download/nginx-1.18.0.tar.gz
        dest: /mnt

#建立目錄
    - name: Create a directory
      file:
        path: /usr/local/nginx
        state: directory

#解壓下載的nginx原始碼包
    - name: Unzip the nginx source package
      unarchive:
        src: /mnt/nginx-1.18.0.tar.gz
        dest: /mnt/

#編譯安裝、先切換目錄再執行  ./configure
    - name: ./configure
      command: ./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx
      args:
        chdir: /mnt/nginx-1.18.0
    - name: make
      command: make
      args:
        chdir: /mnt/nginx-1.18.0
    - name: make install     
      command: make install
      args:
        chdir: /mnt/nginx-1.18.0