在RHEL 9上使用Ansible安装GitLab CE
发布作者:微思网络 发布时间:2023-06-14 浏览量:0次
本文,我将演示在RHEL 9系统上部署GitLab有多么容易。除了使用最新版本的RHEL外,我还将展示如何使用Ansible将部署过程自动化。这意味着将来,您可以使用Ansible自动化将GitLab部署到发布的新版本RHEL上。
一、创建Ansible角色
首先,根据GitLab的安装说明设置一个安装GitLab的角色。以下是角色中包含的目录和文件的列表:
install_gitlab ├── defaults │ └── main.yml ├── handlers │ └── main.yml ├── tasks │ ├── install-gitlab.yml │ ├── install-prerequisites.yml │ ├── main.yml │ ├── set-ce-edition.yml │ ├── set-ee-edition.yml │ └── setup-gitlab-repo.yml └── templates └── gitlab.repo.j2
二、创建Ansible任务
有5个任务文件,从main.yml开始:
--- - name: set vars for community edition ansible.builtin.include_tasks: file: set-ce-edition.yml when: - gitlab_edition == 'community' - name: set vars for enterprise edition ansible.builtin.include_tasks: file: set-ee-edition.yml when: - gitlab_edition == 'enterprise' - name: import pre-tasks ansible.builtin.import_tasks: install-prerequisites.yml - name: setup gitlab repo ansible.builtin.import_tasks: setup-gitlab-repo.yml - name: install gitlab ansible.builtin.import_tasks: install-gitlab.yml
接下来的一组任务文件根据目标GitLab版本定义一些变量。我正在使用社区版,但同时包含两个版本以供您参考。这是 set-ce-edition.yml 文件的内容:
--- - name: set CE vars ansible.builtin.set_fact: edition_abbreviation: ce gitlab_package: gitlab-ce
--- - name: set EE vars ansible.builtin.set_fact: edition_abbreviation: ee gitlab_package: gitlab-ee
--- - name: install prerequisites ansible.builtin.yum: name: - yum-utils - policycoreutils - openssh-server - openssh-clients - postfix register: packages_installed - name: start/enable services ansible.builtin.systemd: name: "{{ service }}" enabled: yes state: started loop_control: loop_var: service loop: - sshd - postfix when: - packages_installed.changed
接着,使用setup-gitlab-repo.yml文件在系统上设置GitLab的软件包仓库。
--- - name: push gitlab repo file ansible.builtin.template: src: templates/gitlab.repo.j2 dest: "/etc/yum.repos.d/gitlab_gitlab-{{ edition_abbreviation }}.repo" owner: root group: root mode: '0644' register: repo_file_pushed - name: clear yum cache ansible.builtin.shell: cmd: yum clean all when: - repo_file_pushed.changed
--- - name: install gitlab ansible.builtin.yum: name: "{{ gitlab_package }}" environment: EXTERNAL_URL: "https://{{ inventory_hostname }}" GITLAB_ROOT_PASSWORD: "{{ gitlab_admin_password }}" notify: - setup_api_token
三、默认值和模板
为了支持这些Ansible任务,需要设置一些其他的东西。
默认值:默认值通常被设置为最后的备选值。这意味着只要变量在其他地方没有被覆盖,就会使用默认值。对于这个角色,设置GitLab版本的默认值。在许多其他具有更高优先级的位置可以定义它,但在这里定义它是有效的。
--- gitlab_edition: 'community'
[gitlab_gitlab-{{ edition_abbreviation }}] name=gitlab_gitlab-{{ edition_abbreviation }} baseurl=https://packages.gitlab.com/gitlab/gitlab-{{ edition_abbreviation }}/el/8/$basearch repo_gpgcheck=1 gpgcheck=1 enabled=1 gpgkey=https://packages.gitlab.com/gitlab/gitlab-{{ edition_abbreviation }}/gpgkey https://packages.gitlab.com/gitlab/gitlab-{{ edition_abbreviation }}/gpgkey/gitlab-gitlab-{{ edition_abbreviation }}-3D645A26AB9FBD22.pub.gpg sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300 [gitlab_gitlab-{{ edition_abbreviation }}-source] name=gitlab_gitlab-{{ edition_abbreviation }}-source baseurl=https://packages.gitlab.com/gitlab/gitlab-{{ edition_abbreviation }}/el/8/SRPMS repo_gpgcheck=1 gpgcheck=1 enabled=1 gpgkey=https://packages.gitlab.com/gitlab/gitlab-{{ edition_abbreviation }}/gpgkey https://packages.gitlab.com/gitlab/gitlab-{{ edition_abbreviation }}/gpgkey/gitlab-gitlab-{{ edition_abbreviation }}-3D645A26AB9FBD22.pub.gpg sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300
处理程序:在GitLab正常运行之后,我喜欢使用个人访问令牌启用API访问。因为您正在安装一个全新的实例,可以在处理程序文件中添加一个任务,为根用户设置访问令牌:
--- - name: set access token for API access ansible.builtin.shell: cmd: > gitlab-rails runner "token = User.find_by_username('root').personal_access_tokens.create(scopes: [:api], name: 'Ansible Automation token'); token.set_token('{{ gitlab_admin_password }}'); token.save!" register: token_create_output listen: - setup_api_token
角色现在已经完成。快速编写一个playbook,并将其保存为???.yml:
--- - name: install gitlab hosts: - all roles: - roles/install_gitlab
--- all: children: gitlab: hosts: gitlab.example.com: ansible_user: tux ansible_password: 'change_me' ansible_become: yes ansible_become_password: 'change_me' gitlab_admin_password: 'change_me'
四:运行Playbook
一切准备就绪。现在是时候运行您的Ansible Playbook了:
当Playbook完成后,访问新GitLab实例的网址,并使用管理员密码登录。
1