NipGeihou's blog NipGeihou's blog
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档

NipGeihou

我见青山多妩媚,料青山见我应如是
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档
  • Linux

    • CentOS

    • Ubuntu

    • 环境安装

    • 常用命令

    • 常用软件

      • 负载均衡 - HAProxy
      • HTTP服务 - Nginx
      • 自动化运维 - Ansible
        • 简介
        • 安装
        • 免密登录(可选)
        • 主机清单(inventory)
          • ini
          • yaml(推荐)
        • 基本命令
          • ansible
        • 常用命令
        • 模块与场景
          • 文件分发 - copy
          • 文件收集 - fetch
          • 定时任务 - cron
          • 执行脚本 - script
          • 解压到目标主机 - unarchive
          • 执行命令 - shell
        • 剧本(playbook)
          • 定义
          • 执行
        • 配置(ansible.cfg)
        • 参考
      • 安全隧道 - gost
    • 最佳实践

    • 通用

  • Docker

  • 云原生

  • Kubernetes

  • KubeSphere

  • K3S

  • 笔记

  • PVE

  • 维修

  • DevOps

  • 云服务

  • 路由器

  • Hyper-V

  • Windows

  • macOS

  • 运维
  • Linux
  • 常用软件
NipGeihou
2024-11-13
目录

自动化运维 - Ansible

# 简介

一个使用 Python 开发的自动化运维工具

  • 批量 SSH
  • 无需客户端:基于 SSH 实现

# 安装

只需安装在本机

pip install ansible

# 检查
ansible ---version

# 免密登录 (可选)

由于是基于 SSH 实现的群控,因此要么密码登录、要么公钥登录,如果是使用 cloud-init 部署的服务器,在模板中添加控制机的公钥到被控机上即可轻松实现,如果没有这么做,可参考 SSH 密钥对 - ssh-keygen

ssh-copy-id username@your_server_ip

# 主机清单 (inventory)

Building an inventory — Ansible Community Documentation (opens new window) ansible 支持 2 个格式的配置:ini、yaml

# ini

创建一个项目目录,用于存放主机清单 (inventory) 配置

mkdir ansible_inventory
cd ansible_inventory
vim inventory.ini

inventory.ini

[myhosts]
# 别名 ansible_host=主机ip ansible_port=ssh端口 ansible_user=用户名 ansible_password=密码
192.0.2.50
192.0.2.51
192.0.2.52

创建 myhosts 组,下面有: 192.0.2.50 、 192.0.2.51 、 192.0.2.52

# 校验配置
ansible-inventory -i inventory.ini --list

# 测试配置连通性
ansible 组名 -m ping -i inventory.ini

# yaml(推荐)

等效上面的 ini

myhosts: # 组名
  hosts:
    my_host_01: # 别名
      ansible_host: 192.0.2.50 # 属性
    my_host_02:
      ansible_host: 192.0.2.51
    my_host_03:
      ansible_host: 192.0.2.52

# 基本命令

# ansible

# -m 模块名
# -u <username>
# -k 回车后,输入密码连接
# -i inventory配置文件路径;不填默认读取/etc/ansible/hosts(hosts是文件)
# --become --ask-become-pass:需要提取操作时使用
# 测试配置连通性
ansible 组名|别名 -m ping -i inventory.ini

# 常用命令

# 查看主机逻辑线程总数
ansible all -m shell -a "lscpu | grep '^CPU(s):' | awk '{print $2}'"

# 查看内存
ansible all -m command -a "free -g"

# 模块与场景

# 文件分发 - copy

# 添加backup=yes:备份目标主机原文件为`原文件+时间`
ansible 组名 -m copy -a 'src=本机文件路径 dest=目标主机路径 owner=文件所有者 group=组 mode=权限(777)'
# ansible 组名 -m copy -a 'src=/tmp/aaa.txt dest=/tmp/bbb.txt'

# 文件收集 - fetch

# flat=yes:不添加的话,会在本地文件路径下创建完整的目标主机路径
ansible 别名 -m fetch -a "src=目标主机路径 dest=本机文件路径"
# ansible host4 -m fetch -a "src=/tmp/file23.txt dest=/root/"

# 定时任务 - cron

ansible 组名 -m cron -a 'name="sync time from ntpserver" minute="*/10" job="/sbin/ntpdate 192.168.174.129 &> /dev/null"'

# 执行脚本 - script

ansible 组名 -m script -a "本机脚本路径"
# ansible 组名 -m script -a "/root/haha.sh"

# 解压到目标主机 - unarchive

ansible 组名 -m unarchive -a 'src=/root/111.tar dest=/home'

# 执行命令 - shell

ansible 组名 -m shell -a '命令'
# ansible 组名 -m shell -a 'hostname'

# 剧本 (playbook)

# 定义

  • yaml 格式文件 playbook.yaml
- name: My first play # 名称
  hosts: myhosts # 受影响的主机别名or组名
  tasks:
   - name: Ping my hosts # 步骤(任务)名称
     ansible.builtin.ping: # 调用ping模块

   - name: Print message
     ansible.builtin.debug:
       msg: Hello world

# 执行

ansible-playbook -i inventory.ini playbook.yaml

# 配置 (ansible.cfg)

/etc/ansible/ansible.cfg

[defaults]
host_key_checking = false

# 参考

  • Getting started with Ansible — Ansible Community Documentation (opens new window)
上次更新: 2024/12/15, 22:15:33
HTTP服务 - Nginx
安全隧道 - gost

← HTTP服务 - Nginx 安全隧道 - gost→

最近更新
01
Docker Swarm
04-18
02
安全隧道 - gost
04-17
03
Solana最佳实践
04-16
更多文章>
Theme by Vdoing | Copyright © 2018-2025 NipGeihou | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式