设置Ubuntu Guest使用aliyun apt镜像源
本文记录如何更改 disk-create.yml 来让 Ubunt Guest 使用阿里云镜像作为 apt source。最终的命令形式如下:
ansible-playbook ./virt-infra.yml --limit kvmhost,ubuntu20Server --extra-vars change_to_ali_ubuntu_source=true
通过在命令行里设置参数 change_to_ali_ubuntu_source=yes 来创建虚拟机的时候进行 ali ubunt apt source 的设置。
1)禁止使用 could init 设置默认的 apt sources
- name: Do not use cloud source
command: >
virt-customize
-a {{ hostvars[groups['kvmhost'][0]].virt_infra_host_image_path | default(virt_infra_host_image_path) }}/{{ inventory_hostname }}-boot.qcow2
--run-command "echo 'apt_preserve_sources_list: true' >> /etc/cloud/cloud.cfg"
register: result_disk_cmd
retries: 2
delay: 2
until: result_disk_cmd is succeeded
become: true
when:
- inventory_hostname not in groups['kvmhost']
- inventory_hostname not in hostvars[groups['kvmhost'][0]].result_all_vms.list_vms
- virt_infra_state != "undefined"
- change_to_ali_ubuntu_source is defined and change_to_ali_ubuntu_source
delegate_to: "{{ groups['kvmhost'][0] }}"
2)使用 aliyun 的 ubuntu apt source 镜像。
- name: Setup Aliyun Source
command: >
virt-customize
-a {{ hostvars[groups['kvmhost'][0]].virt_infra_host_image_path | default(virt_infra_host_image_path) }}/{{ inventory_hostname }}-boot.qcow2
--run-command 'mv /etc/apt/sources.list /etc/apt/sources.list.bak'
--run-command 'echo "deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs) main multiverse restricted universe" >> /etc/apt/sources.list'
--run-command 'echo "deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-backports main multiverse restricted universe" >> /etc/apt/sources.list'
--run-command 'echo "deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-proposed main multiverse restricted universe" >> /etc/apt/sources.list'
--run-command 'echo "deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-security main multiverse restricted universe" >> /etc/apt/sources.list'
--run-command 'echo "deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-updates main multiverse restricted universe" >> /etc/apt/sources.list'
--run-command 'echo "deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs) main multiverse restricted universe" >> /etc/apt/sources.list'
--run-command 'echo "deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-backports main multiverse restricted universe" >> /etc/apt/sources.list'
--run-command 'echo "deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-proposed main multiverse restricted universe" >> /etc/apt/sources.list'
--run-command 'echo "deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-security main multiverse restricted universe" >> /etc/apt/sources.list'
--run-command 'echo "deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-updates main multiverse restricted universe" >> /etc/apt/sources.list'
register: result_disk_cmd
retries: 2
delay: 2
until: result_disk_cmd is succeeded
become: true
when:
- inventory_hostname not in groups['kvmhost']
- inventory_hostname not in hostvars[groups['kvmhost'][0]].result_all_vms.list_vms
- virt_infra_state != "undefined"
- change_to_ali_ubuntu_source is defined and change_to_ali_ubuntu_source
delegate_to: "{{ groups['kvmhost'][0] }}"
3)删除已存在的 Guest 并重新创建。这个虚拟使用 Ali 源并跟随宿主机启动时自动启动。
[devops@localhost virt-infra-ansible]$ ansible-playbook ./virt-infra.yml --limit kvmhost,ubuntu20Server --extra-vars virt_infra_state=undefined
[devops@localhost virt-infra-ansible]$ ansible-playbook ./virt-infra.yml --limit kvmhost,ubuntu20Server --extra-vars change_to_ali_ubuntu_source=true --extra-vars virt_infra_autostart=yes
安装 Docker 环境
[devops@localhost virt-infra-ansible]$ ansible-playbook ./install_docker.yml –limit ubuntu20Server -vv –extra-vars docker_version=5:20.10.4
3-0ubuntu-focal
参考:
关于 virt-customize 命令
https://libguestfs.org/virt-customize.1.html
https://ywnz.com/linuxjc/5680.html
关于 Ansible yaml 中 command
https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html