ansible干啥用的就不多介绍了,这篇文章主要在说ansible的安装、开局配置、免密登录。

ansible安装

  1. 查看系统版本
cat /etc/openEuler-latest

输出内容如下:

openeulerversion=openEuler-24.03-LTS

compiletime=2024-05-27-21-31-28

gccversion=12.3.1-30.oe2403

kernelversion=6.6.0-28.0.0.34.oe2403

openjdkversion=1.8.0.412.b08-5.oe2403

  1. 清除软件库缓存
dnf clean all
  1. 建议软件库缓存
dnf makecache
  1. 安装epel-release软件仓

    1. 下载对应版本epel-release的软件仓库
# 不同系统版本需要安装不同的epel-release
wget https://mirrors.aliyun.com/repo/epel-testing.repo
2. 重新建立软件库索引
mv epel-testing.repo /etc/yum.repo.d/
dnf clean all
dnf makecache
  1. 安装ansible
dnf -y install ansible

等待安装完成即可

开局配置

  1. 常用文件介绍

/etc/ansible/hosts ## 用于存放需要批量管理的主机IP或主机名称

/etc/ansible/ansible.cfg ## 该文件为ansible的主要配置文件

  1. 添加主机到ansible
192.168.0.10    ansible_ssh_pass=主机密码        ansible_ssh_user=主机账号
192.168.0.11 ansible_ssh_pass=主机密码 ansible_ssh_user=主机账号
192.168.0.12 ansible_ssh_pass=主机密码 ansible_ssh_user=主机账号

ansible_ssh_pass:远程主机登录密码

ansible_ssh_user:远程主机登录账号

  1. 远程执行ping命令,会发现执行报错
ansible all -m ping

输出内容如下:

192.168.0.10 | FAILED! => {

"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support

this. Please add this host's fingerprint to your known_hosts file to

manage this host."

}

192.168.0.11 | FAILED! => {

"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support

this. Please add this host's fingerprint to your known_hosts file to

manage this host."

}

192.168.0.12 | FAILED! => {

"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support

this. Please add this host's fingerprint to your known_hosts file to

manage this host."

}

出现这个问题主要是因为ansible默认是没有开启账号密码登录的,默认采用证书登录,只需要在配置文件中把证书登录关闭就可以执行成功了。

进入/etc/ansible/ansible.cfg文件,将host_key_checking = False取消注释或者增加该内容即可

再次重新执行就不会有问题了,成功后输出内容如下

192.168.0.11 | SUCCESS => {

"ansible_facts": {

    "discovered_interpreter_python": "/usr/bin/python"

},

"changed": false,

"ping": "pong"

}

192.168.0.10 | SUCCESS => {

"ansible_facts": {

    "discovered_interpreter_python": "/usr/bin/python"

},

"changed": false,

"ping": "pong"

}

192.168.0.12 | SUCCESS => {

"ansible_facts": {

    "discovered_interpreter_python": "/usr/bin/python"

},

"changed": false,

"ping": "pong"

}

配置免密登录

  1. 生成密钥
ssh-keygen

一路回车即可,输出内容如下:

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa

Your public key has been saved in /root/.ssh/id_rsa.pub

The key fingerprint is:

SHA256:+RGyyNnrIHOLllk+e2hpNyTmxjBZkMY5vvDmTGuEh5g root@ecs-5352

The key's randomart image is:

+---[RSA 3072]----+

| . o |

| B |

| o o . . |

| . ...+ + . |

| o = ++ S . |

|E o @ + .o . |

| Bo%o=. . |

| O=@++ |

| o.+o=.. |

+----[SHA256]-----+

  1. 编写playbook脚本文件
- hosts: # 主机组
remote_user: # 用户名
tasks:
- name: push ansible key
authorized_key: user=root key="{{ lookup('file' ,'密钥存放位置')}}" state=present

示例:

- hosts: all
remote_user: root
tasks:
- name: push ansible key
authorized_key: user=root key="{{ lookup('file' ,'/root/.ssh/id_rsa.pub')}}" state=present
  1. 执行playbook脚本文件
ansible-playbook push_key.yml

输出结果如下表示执行成功:

[root@ecs-5352 yml]# ansible-playbook push_key.yml

PLAY [all]


TASK [Gathering Facts]


ok: [192.168.0.10]

ok: [192.168.0.12]

ok: [192.168.0.11]

TASK [push ansible key]


changed: [192.168.0.10]

changed: [192.168.0.12]

changed: [192.168.0.11]

PLAY RECAP


192.168.0.10 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

192.168.0.11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

192.168.0.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

  1. 测试是否可以免密

    1. 将ansible.cfg配置文件中的host_key_checking = False注释掉

2. 删除hosts文件主机后面的用户名和密码

3. 测试执行ping命令
ansible all -m ping

输出结果如下:

192.168.0.10 | SUCCESS => {

"ansible_facts": {

    "discovered_interpreter_python": "/usr/bin/python"

},

"changed": false,

"ping": "pong"

}

192.168.0.12 | SUCCESS => {

"ansible_facts": {

    "discovered_interpreter_python": "/usr/bin/python"

},

"changed": false,

"ping": "pong"

}

192.168.0.11 | SUCCESS => {

"ansible_facts": {

    "discovered_interpreter_python": "/usr/bin/python"

},

"changed": false,

"ping": "pong"

}

  1. 再次测试

直接在ansible主机上,使用ssh命令测试是否可以免密登录

ssh root@192.168.0.11

无需输入密码即可登录成功

ansible开局配置-openEuler的更多相关文章

  1. Ansible自动化配置详解

    第1章 Ansible基本概述 1.1 ansible是一个配置管理系统configuration management system, 你只需要可以使用ssh访问你的服务器或设备就行. 1.安装软件 ...

  2. Ansible安装配置及命令使用详解

    Ansible和saltstack目前市面上一些其它的项目管理工具有很大的不同,它的设计初衷就是为了更方便.快捷的进行配置管理.它易于安装和使用.语法也非常简单易学.你可以用Ansible将平常复杂的 ...

  3. Ansible 使用配置

    1.配置 /etc/ansible/hosts 文件,添加被管控主机ip #vim /etc/ansible/hosts   文件末尾添加组[group1]和被管控主机的IP [group1] 192 ...

  4. CentOS 9 开局配置

    CentOS 9 开局配置 CentOS 9 发布有几年了,一直没有尝试使用,CentOS 9 有一些变动. 查看系统基础信息 # 查看系统基础信息 [root@chenby ~]# neofetch ...

  5. Ansible安装配置Nginx

    一.思路 现在一台机器上编译安装好nginx.打包,然后在用ansible去下发 cd /etc/ansible 进入ansible配置文件目录 mkdir roles/{common,install ...

  6. Ansible安装配置及使用

    一.Ansible特点 1.不需要安装客户端,通过sshd通信 2.基于模块工作,模块可以由任何序言开发 3.不仅支持命令行使用模块,也支持编写yaml格式的playbook 4.支持sudo 5.有 ...

  7. Ansible安装配置

    Ansible工具的安装与配置 Ansible基于SSH,不需要在远程端安装任何软件,只需要在管理端安装ansible及其组件即可. Ansible使用前提是已配置ssh密钥免登陆. 一.安装组件: ...

  8. Ansible(二) - 配置及命令简介

    Ⅰ. Ansible Inventory Hosts文件配置 # mkdir /etc/ansible # touch /etc/ansible/hosts # cat /etc/hosts 127. ...

  9. 1、Ansible安装配置

    ansible介绍: Ansible是一款基于Python开发的自动化运维工具,主要是实现批量系统配置.批量程序部署.批量运行命令.批量执行任务等等诸多功能.Ansible是一款灵活的开源工具,能够很 ...

  10. ansible常用配置

    1.什么是Ansible 部署参考连接:http://www.ansible.com.cn/ ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfeng ...

随机推荐

  1. C#学习日记

    2023年9月9日 工具visual stdio 2019 窗口名称修改 lable标签 button 点击事件 点击换颜色 formLearn.ActiveForm.BackColor = Colo ...

  2. 折腾 Quickwit,Rust 编写的分布式搜索引擎 - 从不同的来源摄取数据

    摄取 API 在这节教程中,我们将介绍如何使用 Ingest API 向 Quickwit 发送数据. 要跟随这节教程,您需要有一个本地的 Quickwit 实例正在运行. https://quick ...

  3. Linux 主流桌面环境

    GNOME KDE Xfce Ubuntu 使用 GNOME 作为桌面环境. 基于 KDE Plasma 开发的 Ubuntu 发行版:Kubuntu 基于 Xfce 开发的 Ubuntu 发行版:X ...

  4. Java并发编程之验证volatile指令重排-理论篇

    Java并发编程之验证volatile指令重排-理论篇 Java并发包下的类中大量使用了volatile关键字.通过之前文章介绍,大家已经知道了volatile的三大特性:共享变量可见性:不保证原子性 ...

  5. JSP中的JSTL 标签库

    目录 JSTL 标签库 JSTL 标签库的使用步骤 core 核心库使用 <c:set /> (使用很少) <c:if /> <c:choose><c:whe ...

  6. Allen基因图谱:python Aabgen的安装

    1. abagen 使用教程的官方链接:abagen: A toolbox for the Allen Brain Atlas genetics data - abagen 0.1.3-doc+0.g ...

  7. MYSQL数据库设计1

    简单记录一下如何设计MySQL数据库 1.画出概念模型,概念模型是一个业务关系对照图,不需要定义字段类型什么的,仅仅是用于关系对照,指出对照关系 2.根据概念模型设计逻辑模型,逻辑模型需要包含字段的设 ...

  8. Vue SPA项目如何修改网站标题

    直接贴 门户项目代码 // 全局router 直接挂载路由导航守卫 router.beforeEach((to, from, next) => { if (to.meta.title) { va ...

  9. GPUStack正式发布: 为大模型而生的开源GPU集群管理器

    经过 Seal 研发团队几个月来持续的开发和测试,我们满怀期待及热情地发布新的产品 GPUStack,GPUStack 是一个用于运行 LLM(大型语言模型)的开源 GPU 集群管理器.尽管如今大语言 ...

  10. 从零开始掌握 Kubernetes:Pod 和 Deployment 的幕后故事

    1. 引言 在如今的技术世界中,随着微服务架构的广泛应用和云原生理念的兴起,应用程序的开发.部署和管理发生了翻天覆地的变化.容器技术的出现使得开发者可以轻松地将应用及其所有依赖打包在一个轻量级.可移植 ...