佈署 repository 伺服器的 Playbook

要怎麼佈署 repository 伺服器呢?

這蠻簡單的,可以參考

受管主機的環境:RHEL 8.2

要做一個 repository 伺服器,第一個要先註冊到紅帽,因為需要從他的 Repository 伺服器下載套件。 第二個是安裝 yum-utils, httpd, createrepo。

我的 Playbook 資料夾路徑安排如下

.
|____files
| |____sync-repo.sh
|____playbook.yml
  • files/sync-repo.sh 是用來同步的 script。
  • playbook.yml 是主要的 playbook。

先來看 playbook.yml

---
- name: Install repo server
  hosts: all
  become: yes

  vars_prompt:
    - name: username
      prompt: "Your Red Hat account username"
      private: no
    - name: password
      prompt: "Your Red Hat account password"
      private: yes

  tasks:
    - name: Register
      redhat_subscription:
        state: present
        # org_id: "{{ organization_key }}"
        # activationkey: "{{ activation_key }}"
        username: "{{ username }}"
        password: "{{ password }}"
        force_register: yes
      when: username | length > 0 and password | length > 0

    - name: Install required packages
      yum:
        name:
          - yum-utils
          - createrepo
          - httpd
        state: present

    - name: Enable Apache
      service:
        name: httpd
        enabled: yes
        state: started

    - name: Install script
      copy:
        src: files/sync-repo.sh
        dest: /usr/local/bin/sync-repo.sh
        mode: 0755

    - name: Install crontab
      cron:
        name: "Sync repositories"
        minute: "0"
        hour: "2"
        job: "/usr/local/bin/sync-repo.sh"
  1. 使用 vars_prompt 來詢問 Red Hat account 的帳號跟密碼
  2. 安裝必要的套件。
  3. 安裝 httpd,然後啟用跟啟動。
  4. 把 script 放到伺服器裡
  5. 新增 cron job。

files/sync-repo.sh 裡面則是

#!/bin/bash
# you can use `yum repolist` to get repositories

WWWROOT=/var/www/html
for repoid in rhel-8-for-x86_64-baseos-rpms rhel-8-for-x86_64-baseos-rpms; do
    reposync --nogpgcheck --download-metadata --repoid=${repoid} -p ${WWWROOT}
done

執行完 playbook,repository 伺服器就安裝好了,下載、同步套件的工作則會等到半夜兩點才執行。若需要立刻執行,可以用下列指令來執行。

ansible -m shell -a "/usr/local/bin/sync-repo.sh" -i <your_host> -u <username> -k