用ansible部署k8s --- kubespray源码详解(一)

k8s运维内功 — kubespray源码详解(一)

Kubespray是一个使用Ansible作为自动化工具的Kubernetes集群安装、升级和配置的开源项目。
作为一个infra工程师或者devops工程师, 为什么要阅读Kubespray源码,以下是阅读Kubespray源码的三个理由:

  1. 深入理解Kubernetes部署过程
    阅读Kubespray的源码可以帮助你深入了解Kubernetes集群是如何被自动化部署和管理的。这包括了解集群的初始化、节点的加入、网络策略的配置、存储设置、认证和授权等关键组件的部署细节。对于想要掌握Kubernetes集群生命周期管理的开发者和运维工程师来说,这是非常宝贵的知识。

  2. 学习Ansible自动化最佳实践
    Kubespray使用Ansible作为其自动化工具,因此其源码中包含了大量Ansible的最佳实践和模式。通过阅读源码,你可以学习如何编写Ansible playbooks、roles以及如何处理复杂的配置任务。这对于任何希望提高自己在配置管理和自动化领域的技能的人来说都是非常有益的。

  3. 自定义和优化Kubernetes集群部署
    如果你需要对Kubernetes集群进行特定的定制或者优化,阅读Kubespray的源码可以帮助你理解如何修改现有的Ansible playbooks和roles来满足你的需求。这可能包括添加新的功能、修改网络配置、集成额外的服务或者优化性能。通过理解源码,你可以更有效地进行定制,确保集群部署符合你的特定要求。

此外,熟悉kubesrpay源码, 可以帮助你参与开源项目如Kubespray,帮助改进项目,同时也是扩展技术视野和建立开源社区人脉的好机会。

本文用到的kubespray版本:

v2.22.2

用到的ansible版本:

ansible [core 2.12.10]
  config file = /root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/ansible.cfg
  configured module search path = ['/root/.nujnus/test_suite/K8s_v2_22_2/install_k8s_v2_22_2/install/kubespray/library']
  ansible python module location = /opt/conda/lib/python3.9/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/conda/bin/ansible
  python version = 3.9.18 (main, Sep 11 2023, 13:41:44) [GCC 11.2.0]
  jinja version = 3.1.2
  libyaml = True

主机基础信息

主机名 ansible_host ansible_user os cpu ram
test1 192.168.121.91 root CentOS8 6c 8g
test2 192.168.121.92 root CentOS8 6c 8g
test3 192.168.121.95 root CentOS8 6c 8g

inventory

test1 ansible_host=192.168.121.91 ansible_user=root ip=192.168.121.91  etcd_member_name=etcd1 
test2 ansible_host=192.168.121.92 ansible_user=root ip=192.168.121.92  etcd_member_name=etcd2 
test3 ansible_host=192.168.121.95 ansible_user=root ip=192.168.121.95  etcd_member_name=etcd3 

[kube_control_plane]
test1
test2
test3

[etcd]
test1
test2
test3

[kube_node]
test1
test2
test3

[calico_rr]

[k8s_cluster:children]
kube_control_plane
kube_node
calico_rr

源码和注释:

以下部分内容为以debug方式逐个调用ansible任务时通过AIAnsible时生成,
AIAnsible的仓库地址为: https://github.com/sunnycloudy/aiansible

<STEP: 1>

kubespray/playbooks/ansible_version.yml #11

[代码和注释:]
11|    - name: "Check {{ minimal_ansible_version }} <= Ansible version < {{ maximal_ansible_version }}"  # 任务名称,用于检查Ansible的版本是否在指定的范围内
12|      assert:  # 断言模块,用于验证条件是否为真
13|        msg: "Ansible must be between {{ minimal_ansible_version }} and {{ maximal_ansible_version }} exclusive"  # 如果断言失败,将显示此消息
14|        that:  # 指定断言的条件
15|          - ansible_version.string is version(minimal_ansible_version, ">=")  # 条件1:Ansible版本至少为minimal_ansible_version
16|          - ansible_version.string is version(maximal_ansible_version, "<")  # 条件2:Ansible版本小于maximal_ansible_version
17|      tags:  # 标签,用于选择执行哪些任务
18|        - check  # 给该任务添加的标签为check

[参数:]

msg: Ansible must be between 2.11.0 and 2.13.0 exclusive
that: ['ansible_version.string is version(minimal_ansible_version, ">=")', 'ansible_version.string is version(maximal_ansible_version, "<")']

<STEP: 2>

kubespray/playbooks/ansible_version.yml #20

[代码和注释:]
20|    - name: "Check that python netaddr is installed"  # 任务名称,检查Python netaddr模块是否已安装
21|      assert:  # 断言模块,用于验证条件是否为真
22|        msg: "Python netaddr is not present"  # 如果断言失败,将显示此消息,提示Python netaddr模块未安装
23|        that: "'127.0.0.1' | ipaddr"  # 指定断言的条件,检查'127.0.0.1'是否是一个有效的IP地址
24|      tags:  # 标签,用于选择执行哪些任务
25|        - check  # 给该任务添加的标签为check

[参数:]

msg: Python netaddr is not present
that: '127.0.0.1' | ipaddr

<STEP: 3>

kubespray/playbooks/ansible_version.yml #28

[代码和注释:]
28|    - name: "Check that jinja is not too old (install via pip)"  # 任务名称,用于检查Jinja版本是否过旧,建议通过pip安装
29|      assert:  # 断言模块,用于验证条件是否为真
30|        msg: "Your Jinja version is too old, install via pip"  # 如果断言失败,将显示此消息,提示Jinja版本过旧,需要通过pip安装
31|        that: "{% set test %}It works{% endset %}{{ test == 'It works' }}"  # 断言条件,检查Jinja模板是否能够正确执行并返回预期结果
32|      tags:  # 标签,用于选择执行哪些任务
33|        - check  # 给该任务添加的标签为check

[参数:]

msg: Your Jinja version is too old, install via pip
that: True

<STEP: 4>

kubespray/roles/kubespray-defaults/tasks/main.yaml #2

[代码和注释:]
2|- name: Configure defaults  # 任务名称,用于配置默认设置
3|  debug:  # 调试模块,用于输出信息
4|    msg: "Check roles/kubespray-defaults/defaults/main.yml"  # 要输出的信息,提示检查默认配置文件的位置
5|  tags:  # 标签,用于选择执行哪些任务
6|    - always  # 给该任务添加的标签为always,表示总是执行此任务

[参数:]

msg: Check roles/kubespray-defaults/defaults/main.yml

<STEP: 5>

kubespray/roles/bootstrap-os/tasks/main.yml #2

[代码和注释:]
2|- name: Fetch /etc/os-release  # 任务名称,用于获取操作系统版本信息
3|  raw: cat /etc/os-release  # 使用raw模块执行shell命令,读取/etc/os-release文件内容
4|  register: os_release  # 将命令执行结果存储在变量os_release中
5|  changed_when: false  # 无论命令是否执行成功,都不标记任务状态为changed
6|  # This command should always run, even in check mode  # 注释说明此命令在检查模式下也应该执行
7|  check_mode: false  # 设置为false,表示即使在检查模式下,此命令也会执行

[参数:]

_raw_params: cat /etc/os-release

<STEP: 6>

kubespray/roles/bootstrap-os/tasks/main.yml #9

[代码和注释:]
9|- include_tasks: bootstrap-centos.yml  # 包含执行CentOS系统初始化的任务
10|  when: '''ID="centos"'' in os_release.stdout_lines or ''ID="ol"'' in os_release.stdout_lines or ''ID="almalinux"'' in os_release.stdout_lines or ''ID="rocky"'' in os_release.stdout_lines or ''ID="kylin"'' in os_release.stdout_lines or ''ID="uos"'' in os_release.stdout_lines or ''ID="openEuler"'' in os_release.stdout_lines  # 当操作系统的ID为centos, ol, almalinux, rocky, kylin, uos, openEuler时执行包含的任务

[参数:]

_raw_params: bootstrap-centos.yml

<STEP: 7>

kubespray/roles/bootstrap-os/tasks/bootstrap-centos.yml #2

[代码和注释:]
2|- name: Gather host facts to get ansible_distribution_version ansible_distribution_major_version  # 任务名称,用于收集主机的事实信息,以便获取操作系统的版本信息
3|  setup:  # 配置模块,用于设置和获取主机信息
4|    gather_subset: '!all'  # 指定不收集所有事实信息,排除所有
5|    filter: ansible_distribution_*version  # 过滤条件,只收集与操作系统版本相关的信息

[参数:]

gather_subset: !all
filter: ansible_distribution_*version
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: setup
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564277.9041831-1370-219571751798710/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 8>

kubespray/roles/bootstrap-os/tasks/bootstrap-centos.yml #7

[代码和注释:]
7|- name: 如果定义了 http_proxy,则向 yum.conf 或 dnf.conf 添加代理设置  # 任务名称,用于添加代理设置
8|  ini_file:  # ini_file 模块,用于编辑配置文件
9|    path: "{{ ( (ansible_distribution_major_version | int) < 8) | ternary('/etc/yum.conf','/etc/dnf/dnf.conf') }}"  # 根据发行版版本选择配置文件路径
10|    section: main  # 配置文件中要编辑的区段
11|    option: proxy  # 要编辑的配置项
12|    value: "{{ http_proxy | default(omit) }}"  # 配置项的值,如果未定义 http_proxy 则使用 omit
13|    state: "{{ http_proxy | default(False) | ternary('present', 'absent') }}"  # 根据 http_proxy 是否定义来决定配置项是存在还是不存在
14|    no_extra_spaces: true  # 确保配置文件中没有额外的空格
15|    mode: 0644  # 设置文件的权限
16|  become: true  # 使用 become 模块,以提升权限执行任务
17|  when: not skip_http_proxy_on_os_packages  # 如果未跳过操作系统包的 http_proxy 设置,则执行此任务

[参数:]

path: /etc/dnf/dnf.conf
section: main
option: proxy
state: absent
no_extra_spaces: True
mode: 420
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: ini_file
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564279.2650752-1469-230973806523141/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 9>

kubespray/roles/bootstrap-os/tasks/bootstrap-centos.yml #91

[代码和注释:]
91|- name: Check presence of fastestmirror.conf  # 任务名称,用于检查fastestmirror.conf文件是否存在
92|  stat:  # 使用stat模块来获取文件状态
93|    path: /etc/yum/pluginconf.d/fastestmirror.conf  # 指定要检查的文件路径
94|    get_attributes: no  # 不获取文件属性
95|    get_checksum: no  # 不获取文件校验和
96|    get_mime: no  # 不获取文件的MIME类型
97|  register: fastestmirror  # 将文件状态信息注册到变量fastestmirror中

[参数:]

path: /etc/yum/pluginconf.d/fastestmirror.conf
get_attributes: False
get_checksum: False
get_mime: False
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: stat
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564280.5849237-1585-125489234597939/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 10>

kubespray/roles/bootstrap-os/tasks/bootstrap-centos.yml #113

[代码和注释:]
113|- name: Install libselinux python package  # 安装libselinux Python包
114|  package:  # 使用package模块
115|    name: "{{ ( (ansible_distribution_major_version | int) < 8) | ternary('libselinux-python','python3-libselinux') }}"  # 根据发行版主版本号选择安装包名
116|    state: present  # 确保包安装状态为存在
117|  become: true  # 使用become模块以提升权限执行安装操作

[参数:]

name: python3-libselinux
state: present

<STEP: 11>

kubespray/roles/bootstrap-os/tasks/main.yml #42

[代码和注释:]
42|- name: Create remote_tmp for it is used by another module  # 创建远程临时目录,因为其他模块需要使用它
43|  file:  # 使用file模块
44|    path: "{{ ansible_remote_tmp | default('~/.ansible/tmp') }}"  # 指定目录路径,如果ansible_remote_tmp未定义则使用默认值
45|    state: directory  # 确保路径是一个目录
46|    mode: 0700  # 设置目录权限为0700,即只有所有者有读写执行权限

[参数:]

path: ~/.ansible/tmp
state: directory
mode: 448
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: file
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564301.3632543-2670-260338308843800/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 12>

kubespray/roles/bootstrap-os/tasks/main.yml #50

[代码和注释:]
50|- name: Gather host facts to get ansible_os_family  # 任务名称,用于收集主机信息以获取操作系统家族信息
51|  setup:  # 设置模块,用于收集主机信息
52|    gather_subset: '!all'  # 指定不收集所有信息
53|    filter: ansible_*  # 指定只收集以ansible_开头的信息,如ansible_os_family

[参数:]

gather_subset: !all
filter: ansible_*
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: setup
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564302.1805813-2744-63069179700213/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 13>

kubespray/roles/bootstrap-os/tasks/main.yml #55

[代码和注释:]
55|- name: 为未配置主机名的主机分配库存主机名(非CoreOS、非Flatcar、非Suse和ClearLinux,非Fedora) # 任务名称,用于为特定操作系统分配主机名
56|  hostname:  # 调用hostname模块
57|    name: "{{ inventory_hostname }}"  # 设置主机名为inventory_hostname变量的值
58|  when:  # 指定任务执行的条件
59|    - override_system_hostname  # 如果override_system_hostname变量为真,则继续执行
60|    - ansible_os_family not in ['Suse', 'Flatcar', 'Flatcar Container Linux by Kinvolk', 'ClearLinux']  # 排除Suse、Flatcar和ClearLinux操作系统
61|    - not ansible_distribution == "Fedora"  # 排除Fedora操作系统
62|    - not is_fedora_coreos  # 如果不是Fedora CoreOS,则继续执行

[参数:]

name: test3
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: hostname
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564303.1082404-2812-74781520497657/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 14>

kubespray/roles/bootstrap-os/tasks/main.yml #94

[代码和注释:]
94|- name: Ensure bash_completion.d folder exists  # 任务名称,确保bash_completion.d目录存在
95|  file:  # 文件模块,用于管理文件、目录和链接
96|    name: /etc/bash_completion.d/  # 指定要管理的目录路径
97|    state: directory  # 指定目录状态,此处为directory表示创建目录
98|    owner: root  # 指定目录的所有者为root用户
99|    group: root  # 指定目录的所属组为root组
100|    mode: 0755  # 设置目录的权限模式为0755,即rwxr-xr-x

[参数:]

name: /etc/bash_completion.d/
state: directory
owner: root
group: root
mode: 493
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: file
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564304.9461253-2941-197619039732109/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 15>

kubespray/playbooks/facts.yml #20

[代码和注释:]
20|    - name: Gather minimal facts  # 任务名称,用于收集最基本的主机信息
21|      setup:  # setup模块,用于收集主机信息
22|        gather_subset: '!all'  # 指定收集信息的子集,'!all'表示不收集所有信息,只收集最基本的信息

[参数:]

gather_subset: !all
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: setup
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564305.6820738-3007-58846731917913/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 16>

kubespray/playbooks/facts.yml #29

[代码和注释:]
29|    - name: Gather necessary facts (network)  # 任务名称,用于收集网络相关的系统信息
30|      setup:  # 使用setup模块来收集系统信息
31|        gather_subset: '!all,!min,network'  # 指定只收集网络相关的信息,排除所有(all)和最小(min)信息
32|        filter: "ansible_*_ipv[46]*"  # 过滤条件,只收集以ansible_开头,包含ipv4或ipv6的系统信息

[参数:]

gather_subset: !all,!min,network
filter: ansible_*_ipv[46]*
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: setup
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564306.6219034-3076-7528483672975/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 17>

kubespray/playbooks/facts.yml #37

[代码和注释:]
37|    - name: Gather necessary facts (hardware)  # 任务名称,用于收集必要的硬件信息
38|      setup:  # 调用setup模块,用于收集系统信息
39|        gather_subset: '!all,!min,hardware'  # 指定只收集硬件相关的信息,排除所有(all)和最小(min)集
40|        filter: "ansible_*total_mb"  # 过滤条件,只收集以ansible_开头且包含total_mb的硬件信息

[参数:]

gather_subset: !all,!min,hardware
filter: ansible_*total_mb
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: setup
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564307.6525276-3148-19931225924080/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 18>

kubespray/roles/kubespray-defaults/tasks/main.yaml #2

[代码和注释:]
2|- name: Configure defaults  # 任务名称,用于配置默认设置
3|  debug:  # 调试模块,用于输出信息
4|    msg: "Check roles/kubespray-defaults/defaults/main.yml"  # 要输出的信息,提示检查默认配置文件的位置
5|  tags:  # 标签,用于选择执行哪些任务
6|    - always  # 给该任务添加的标签为always,表示总是执行此任务

[参数:]

msg: Check roles/kubespray-defaults/defaults/main.yml

<STEP: 19>

kubespray/roles/kubespray-defaults/tasks/fallback_ips.yml #17

[代码和注释:]
17|- name: create fallback_ips_base  # 创建一个名为fallback_ips_base的事实
18|  set_fact:  # 设置一个事实
19|    fallback_ips_base: |  # 定义一个多行字符串,用于存储IP地址列表
20|      ---  # YAML文档的开头
21|      {% for item in (groups['k8s_cluster']|default([]) + groups['etcd']|default([]) + groups['calico_rr']|default([]))|unique %}  # 遍历三个组的主机名列表,并去重
22|      {% set found = hostvars[item].get('ansible_default_ipv4') %}  # 获取每个主机的ansible_default_ipv4变量
23|      {{ item }}: "{{ found.get('address', '127.0.0.1') }}"  # 为每个主机设置IP地址,如果未找到则使用127.0.0.1
24|      {% endfor %}  # 结束循环
25|  delegate_to: localhost  # 指定任务在localhost上执行
26|  connection: local  # 使用本地连接
27|  delegate_facts: yes  # 允许将本地事实传递给ansible控制节点
28|  become: no  # 不以root权限执行任务
29|  run_once: yes  # 该任务只执行一次,无论有多少个主机参与任务执行

[参数:]

fallback_ips_base: ---
test1: "10.0.2.15"
test2: "10.0.2.15"
test3: "10.0.2.15"


<STEP: 20>

kubespray/roles/kubespray-defaults/tasks/fallback_ips.yml #31

[代码和注释:]
31|- name: set fallback_ips  # 定义一个任务名称,用于设置备用IP地址
32|  set_fact:  # 使用set_fact模块设置一个事实
33|    fallback_ips: "{{ hostvars.localhost.fallback_ips_base | from_yaml }}"  # 将localhost主机的fallback_ips_base变量解析为YAML格式,并赋值给fallback_ips事实变量

[参数:]

fallback_ips: {'test1': '10.0.2.15', 'test2': '10.0.2.15', 'test3': '10.0.2.15'}

<STEP: 21>

kubespray/roles/adduser/tasks/main.yml #2

[代码和注释:]
2|- name: User | Create User Group # 任务名称,用于创建用户组
3|  group: # 定义一个group模块
4|    name: "{{ user.group|default(user.name) }}" # 设置组的名称,如果user.group未定义,则默认为user.name
5|    system: "{{ user.system|default(omit) }}" # 设置组是否为系统组,如果user.system未定义,则省略此参数

[参数:]

name: kube-cert
system: True
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: group
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564311.7094977-3443-62561575767024/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 22>

kubespray/roles/adduser/tasks/main.yml #7

[代码和注释:]
7|- name: User | Create User  # 任务名称,用于创建用户
8|  user:  # 指定操作为用户管理
9|    comment: "{{ user.comment|default(omit) }}"  # 用户注释,如果未定义则省略
10|    create_home: "{{ user.create_home|default(omit) }}"  # 是否创建用户主目录,如果未定义则省略
11|    group: "{{ user.group|default(user.name) }}"  # 用户所属的组,如果未定义则默认为用户名
12|    home: "{{ user.home|default(omit) }}"  # 用户主目录路径,如果未定义则省略
13|    shell: "{{ user.shell|default(omit) }}"  # 用户登录使用的shell,如果未定义则省略
14|    name: "{{ user.name }}"  # 用户名
15|    system: "{{ user.system|default(omit) }}"  # 是否是系统用户,如果未定义则省略
16|  when: user.name != "root"  # 条件判断,如果不是root用户则执行上述用户创建操作

[参数:]

comment: Kubernetes user
create_home: False
group: kube-cert
shell: /sbin/nologin
name: kube
system: True
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: user
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564312.5997043-3514-61120220482937/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 23>

kubespray/roles/kubernetes/preinstall/tasks/0010-swapoff.yml #2

[代码和注释:]
2|- name: Remove swapfile from /etc/fstab  # 任务名称,用于从 /etc/fstab 中移除交换文件配置
3|  mount:  # 使用 mount 模块来管理挂载点
4|    name: "{{ item }}"  # 挂载点名称,使用变量 item 来指定
5|    fstype: swap  # 文件系统类型,设置为 swap
6|    state: absent  # 挂载状态,设置为 absent 表示要移除挂载
7|  loop:  # 开始循环
8|    - swap  # 循环的第一个元素,指定为 swap
9|    - none  # 循环的第二个元素,指定为 none,表示不挂载任何文件系统
# 注:以上代码段中,item 变量的值应在外部定义,且应包含 'swap' 和 'none' 两个元素,以确保正确执行循环。

[参数:]

name: {{ item }}
fstype: swap
state: absent

<STEP: 24>

kubespray/roles/kubernetes/preinstall/tasks/0010-swapoff.yml #12

[代码和注释:]
12|- name: check swap  # 任务名称,用于检查交换空间的状态
13|  command: /sbin/swapon -s  # 执行命令,用于列出当前系统的所有交换空间使用情况
14|  register: swapon  # 将命令执行的结果注册到变量swapon中,供后续使用
15|  changed_when: no  # 设置此任务不会标记为已改变状态,无论命令执行结果如何

[参数:]

_raw_params: /sbin/swapon -s
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: ansible.legacy.command
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564315.0138195-3694-170592716662214/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 25>

kubespray/roles/kubernetes/preinstall/tasks/0020-set_facts.yml #24

[代码和注释:]
24|- name: check if booted with ostree  # 任务名称,用于检查系统是否使用ostree引导
25|  stat:  # 使用stat模块来获取文件或目录的状态信息
26|    path: /run/ostree-booted  # 指定要检查的文件路径
27|    get_attributes: no  # 不获取文件属性
28|    get_checksum: no  # 不获取文件的校验和
29|    get_mime: no  # 不获取文件的MIME类型
30|  register: ostree  # 将stat模块的结果注册到变量ostree中

[参数:]

path: /run/ostree-booted
get_attributes: False
get_checksum: False
get_mime: False
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: stat
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564317.2394493-3846-264816096711092/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 26>

kubespray/roles/kubernetes/preinstall/tasks/0020-set_facts.yml #32

[代码和注释:]
32|- name: set is_fedora_coreos  # 设置任务名称为"set is_fedora_coreos"
33|  lineinfile:  # 使用lineinfile模块,用于管理文件中的行
34|    path: /etc/os-release  # 指定要操作的文件路径
35|    line: "VARIANT_ID=coreos"  # 指定要添加或修改的行内容
36|    state: present  # 指定文件中该行的状态,这里为"present",表示确保该行存在
37|  check_mode: yes  # 在检查模式下运行,不实际修改文件,只检查是否需要修改
38|  register: os_variant_coreos  # 将任务的结果注册到变量os_variant_coreos中
39|  changed_when: false  # 指定即使任务执行了,也不标记为已更改状态

[参数:]

path: /etc/os-release
line: VARIANT_ID=coreos
state: present
_ansible_check_mode: True
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: lineinfile
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564317.8996027-3898-154144620889147/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 27>

kubespray/roles/kubernetes/preinstall/tasks/0020-set_facts.yml #41

[代码和注释:]
41|- name: set is_fedora_coreos  # 定义一个任务名称,用于设置is_fedora_coreos变量
42|  set_fact:  # 使用set_fact模块来设置或更新变量
43|    is_fedora_coreos: "{{ ostree.stat.exists and os_variant_coreos is not changed }}"  # 将变量is_fedora_coreos设置为判断ostree.stat.exists和os_variant_coreos是否未变更的逻辑表达式的结果

[参数:]

is_fedora_coreos: False

<STEP: 28>

kubespray/roles/kubernetes/preinstall/tasks/0020-set_facts.yml #45

[代码和注释:]
45|- name: check resolvconf  # 任务名称,用于检查resolvconf命令是否存在
46|  command: which resolvconf  # 执行的命令,用于查找resolvconf命令的位置
47|  register: resolvconf  # 将命令执行的结果注册到resolvconf变量中
48|  failed_when: false  # 设置失败时不标记任务为失败
49|  changed_when: false  # 设置任务执行后不标记为已更改
50|  check_mode: no  # 在检查模式下不执行实际的命令操作,只检查任务是否能够执行成功

[参数:]

_raw_params: which resolvconf
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: ansible.legacy.command
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564318.7520318-3982-186997746996679/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 29>

kubespray/roles/kubernetes/preinstall/tasks/0020-set_facts.yml #52

[代码和注释:]
52|- name: check existence of /etc/resolvconf/resolv.conf.d  # 检查文件 /etc/resolvconf/resolv.conf.d 是否存在
53|  stat:  # 使用stat模块来获取文件状态
54|    path: /etc/resolvconf/resolv.conf.d  # 指定要检查的文件路径
55|    get_attributes: no  # 不获取文件属性
56|    get_checksum: no  # 不获取文件的校验和
57|    get_mime: no  # 不获取文件的MIME类型
58|  failed_when: false  # 即使检查失败,也不标记整个任务为失败
59|  register: resolvconfd_path  # 将结果注册到变量resolvconfd_path中

[参数:]

path: /etc/resolvconf/resolv.conf.d
get_attributes: False
get_checksum: False
get_mime: False
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: stat
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564319.5724564-4029-248396980589932/
_ansible_remote_tmp: ~/.ansible/tmp

<STEP: 30>

kubespray/roles/kubernetes/preinstall/tasks/0020-set_facts.yml #61

[代码和注释:]
61|- name: check status of /etc/resolv.conf  # 任务名称,用于检查文件/etc/resolv.conf的状态
62|  stat:  # 使用stat模块,用于获取文件或目录的状态
63|    path: /etc/resolv.conf  # 指定要检查的文件路径
64|    follow: no  # 不跟随符号链接,即使文件是符号链接,也检查符号链接本身而不是它指向的目标
65|    get_attributes: no  # 不获取文件的额外属性
66|    get_checksum: no  # 不获取文件的校验和
67|    get_mime: no  # 不获取文件的MIME类型
68|  failed_when: false  # 即使任务失败,也不会标记为失败,即不会中断后续任务的执行
69|  register: resolvconf_stat  # 将任务的结果注册到变量resolvconf_stat中,以便后续使用

[参数:]

path: /etc/resolv.conf
follow: False
get_attributes: False
get_checksum: False
get_mime: False
_ansible_check_mode: False
_ansible_no_log: False
_ansible_debug: False
_ansible_diff: False
_ansible_verbosity: 0
_ansible_version: 2.12.10
_ansible_module_name: stat
_ansible_syslog_facility: LOG_USER
_ansible_selinux_special_fs: ['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat']
_ansible_string_conversion_action: warn
_ansible_socket: None
_ansible_shell_executable: /bin/sh
_ansible_keep_remote_files: False
_ansible_tmpdir: /root/.ansible/tmp/ansible-tmp-1717564320.2179492-4095-11302077309011/
_ansible_remote_tmp: ~/.ansible/tmp

相关推荐

  1. ansible部署k8s --- kubespray详解()

    2024-06-08 12:26:03       7 阅读
  2. 使用ansible命令部署k8s集群

    2024-06-08 12:26:03       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 12:26:03       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 12:26:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 12:26:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 12:26:03       18 阅读

热门阅读

  1. websocket 前端项目js示例

    2024-06-08 12:26:03       9 阅读
  2. Vue Router——hash模式和 history模式

    2024-06-08 12:26:03       11 阅读
  3. Elasticsearch 认证模拟题 - 10

    2024-06-08 12:26:03       10 阅读
  4. TCP和udp能使用同一个端口通讯吗

    2024-06-08 12:26:03       9 阅读
  5. 设计模式总结

    2024-06-08 12:26:03       6 阅读
  6. UVa1116/LA2429 Puzzle

    2024-06-08 12:26:03       5 阅读