Ansible自动化部署K8s集群一 Ansible的基础使用实战

发布于:2025-02-12 ⋅ 阅读:(8) ⋅ 点赞:(0)

一、Ansible介绍

        1.安装ansible:        yum  install  ansible -y

        2.ansible的架构图:

        3.ansible四部分:

        inventory:ansible管理的主机信息,包括ip地址、ssh端口、账号和密码等

        modules:任务均由模块完成

        plugins:增加ansible的核心功能

        playbooks:模块化当以一些列任务供外部调用

        4.主机清单(示例代码):

[webservers]
192.168.142.152
192.168.142.153
www.xiao.org

        5.命令行使用:ad-hoc可以输入内容快速执行某个操作

          远程连接主机认证:

[webservers]
192.168.142.152  ansible_ssh_user=root ansible_ssh_pass='xiao'
192.168.142.153  ansible_ssh_user=root ansible_ssh_pass='xiao'

          ssh密钥对认证(ansible是基于ssh进行远程连接执行命令:

[webservers]
192.168.142.152  ansible_ssh_user=root ansible_ssh_key=/root/.ssh/id_rsa
192.168.142.153  ansible_ssh_user=root

          上述的密钥信息也可以在ansible.cfg配置文件中指定

          另外,也可以先建立起控制主机和受控主机之间的ssh的免密连接,通过在控制主机上创建一对密钥对,将公钥通过ssh_copy_id  root@192.168.142.152/3传递到被控主机上,可以顺利执行ansible的ad-hoc命令。

        6.ansible命令行常用的选项:

          -C / --check        playbook.yml的语法检查,不执行任何操作

          -e  var=test         设置附加变量 key=value

          --user=xiao         ssh连接的用户,默认为none

          -k                        ssh连接用户的密码

          -b / --become       提权,默认为root

          -K                          提权密码

        7.命令行使用: 

            ansible  all  -m  ping                     网络连通性测试

            ansible  all  -m  shell    -a"ls  /root"  -u  root  -k            在命令行执行脚本(查看/root信息)

            ansible  webservers  -m  copy  -a  "src=/etc/hosts  dest=/tmp/host"          指定webservers主机组中的主机,使得将本主机的指定文件复制到指定主机组的指定目录下。

二、ansible的常用模块

        1.shell(在远程主机上执行脚本):

            ansible  webservers -m  shell    -a  "ls  /root" 

            其中如果执行多条shell语句可以使用分号将其分隔开。

        2.copy(将文件复制到远程主机):

            ansible  webservers  -m  copy  -a  "src=/etc/hosts  dest=/tmp/host  mode=777  owner=xiao group=xiao  backup=yes"

            上述指令的作用时将指定的文件复制到远程主机上并指定复制后的主机的属主和文件的权限,其中的backup表示如果远程主机上该文件存在,会将其创建为一个备份。

        3.file(管理文件和文件属性):

            ansible webservers  -m  file  -a "path=/tmp/hosts  state=absent"

            上述命令表示删除远程主机上的path路径的文件或者递归删除目录

            上述中state参数的值决定对文件进行什么操作,其中absent表示删除,present表示创建

        4.yum(软件包管理):

            ansible  webservers  -m yum  -a  "name=httpd  state=latest"

           该条命令表示在远程主机上安装最新版本的http服务,其中state可以取absent(卸载)  present(安装yum源中的软件包)  latest(安装最新的软件)

        5.systemd(管理服务):

            ansible  webservers  -m  systemd  -a  "name=httpd  state=started  enabled=yes"

            该命令表示启动httpd服务并使其开机自启动,也可以设定其服务状态为stopped  restarted  reloaded

        6.unarchive(解压):

            ansible  webservers  -m unarchive  -a "src=test.tar.gz  dest=/tmp"

            该命令表示将本机文件的压缩文件解压到远程主机的指定目录上

        7.debug(执行过程中打印语句):

            ansible  webservers  -m  debug  -a  "var=hostvars[inventory_hostname]"

            该命令表示打印远程主机的所有变量

三、Playbook的简单操作

        1.主机和用户: 

- hosts: webservers
  remote_user: xiao
  become: yes
  become_user: yes

          2.定义变量:

#命令行传递:
-e var=test
#主机变量和组变量(在inventory中定义变量)
  #主机变量
[webservers]
192.168.142.152  ansible_ssh_user=root  hostname=web1
192.168.142.153  ansible_ssh_user=root  hostname=web2
  #主机组变量
[webservers:vars]
ansible_ssh_user=root

          单文件存储:

           /etc/ansible/group_vars/all.yml        对所有的主机有效

           /etc/ansible/host_vars/webservers.yml        只对webservers主机组的主机生效

          在playbook中自定义变量:

           - hosts: webservers

              vars:

                  http_port: 80

                   server_name: www.xiao.org

           register变量(将其他命令的运行结果保存下来作为变量的值):

           -shell:  /etc/passwd

             register: result

           -debug:

                var: result

        3.任务列表:

tasks:
- name: 安装nginx最新版
  yum: pkg=nginx  state=latest

        4.任务控制:

        可以将大的playbook中的多个任务进行标记,可以让其只执行有特殊标记的任务,或者让其跳过执行有特殊标记的任务。

tasks:
- name; 安装nginx最新版
  yum: pkg=nginx state=latest
  tags: install
- name: 写入nginx的配置文件
  template: src=/test/nginx.conf dest=/etc/nginx/nginx.conf
  tags: config

        ansible-playbook  test.yml --tags "install"        表示只执行带有install标签的任务

        ansible-playbook test.yml  --skip-tags "install"        表示跳过带有install标签的任务

        5.流程控制:

tasks:
- name: 仅在192.168.142.152上执行任务
  debug; msg= {
  
  {ansible_default_ipv4.address}}
  when: ansible_default_ipv4.address == '192.168.142.152'

          循环:

tasks:
-name; 批量创建用户
 user: name={
  
  { item }} state=present  groups=xiao
 with_items:
    - testuser1
    - testuser2

        常用的循环语句:

        with_items        标准循环

        with_fileglob        遍历目录文件

        with_dict        遍历字典