ansible脚本用于在无互联网访问的CentOS服务器上安装Docker仓库。

huangapple go评论65阅读模式
英文:

ansible script to install docker repo on centos server with no internet access

问题

寻找一个 Ansible 脚本来在没有互联网访问权限的 CentOS 8 服务器上安装 Docker 软件包。我已经在我的测试服务器上尝试了以下内容(该服务器有互联网访问权限),但实际服务器没有互联网访问权限,正在寻找解决方案。

---
- hosts: localhost
  become: true
  tasks:
  - name: 安装 yum-utils
    yum:
      name: yum-utils
      state: latest

  - name: 添加 Docker 仓库
    get_url:
      url: https://download.docker.com/linux/centos/docker-ce.repo
      dest: /etc/yum.repos.d/docker-ce.repo
    become: yes

不要翻译代码部分,只翻译注释和描述。

英文:

looking for an ansible script to install docker packages on the centos8 server with no internet access.
I have tried the below on my test server(which has internet access) but the actual server doesn't have access to the internet and looking out for options.

---
- hosts: localhost
  becomes: true
  tasks:
  - name: Install yum utils
  yum:
    name: yum-utils
    state: latest

  - name: Add Docker repo
  get_url:
    url: https://download.docker.com/linux/centos/docker-ce.repo
    dest: /etc/yum.repos.d/docer-ce.repo
  become: yes

答案1

得分: 0

你可以在playbook中从ansible控制器提供RPM,然后使用yum模块安装它。

- name: 从本地文件安装nginx rpm
  ansible.builtin.yum:
    name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    state: present
英文:

You could provide the RPM from the ansible controller as part of your playbook then install it using the yum module.

> yaml
> - name: Install nginx rpm from a local file
> ansible.builtin.yum:
> name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
> state: present
>

答案2

得分: 0

如果只有目标服务器没有连接到互联网,您可以从控制器获取文件并将其推送到目标服务器:

- name: 从本地获取 Docker 仓库定义
  ansible.builtin.get_url:
    url: https://download.docker.com/linux/centos/docker-ce.repo
    dest: /tmp/docer-ce.repo
  changed_when: false
  delegate_to: localhost
  run_once: true

- name: 将仓库文件复制到目标服务器
  ansible.builtin.copy:
    src: /tmp/docer-ce.repo
    dest: /etc/yum.repos.d/docer-ce.repo
  become: yes

在上述代码中:

  • 我考虑从 URL 获取参考文件到控制器对于幂等性而言是一个无事件,这就是为什么有 changed_when: false
  • 如果复制任务需要创建文件,或者如果目标服务器上的文件与获取的参考文件不同,它将报告 change
  • run_once: true 确保文件只被获取一次,无论在播放循环中有多少目标主机。复制任务将针对每个目标运行,并推送具有相同内容的文件。

如果控制器和目标服务器都没有连接到互联网,则必须在复制之前在控制器上以某种方式获取仓库文件(并随时间进行维护)。

英文:

If only the target server isn't connected to internet, you can get the file from the controller and push it to the target:

- name: Get Docker repo definition locally
  ansible.builtin.get_url:
    url: https://download.docker.com/linux/centos/docker-ce.repo
    dest: /tmp/docer-ce.repo
  changed_when: false
  delegate_to: localhost
  run_once: true

- name: Copy the repo file to target(s)
  ansible.builtin.copy:
    src: /tmp/docer-ce.repo
    dest: /etc/yum.repos.d/docer-ce.repo
  become: yes

In the above:

  • I considered getting the ref file from url to controller being a non-event as far as idempotence is concerned, hence why the changed_when: false.
  • The copy task will report change if the file has to be created or was modified if it differs on target from the fetched reference.*
  • run_once: true ensures the file is fetched only once whatever the number of target hosts in your play loop. The copy task will run for each target and push the file with the same content

If both the controller and target are not connected to internet, you will have to get the repo file on the controller somehow before copying (and maintain it over time)

huangapple
  • 本文由 发表于 2023年2月16日 05:10:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定