ansible开局配置-openEuler

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

ansible安装

  1. 查看系统版本
powershell 复制代码
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. 清除软件库缓存
powershell 复制代码
dnf clean all
  1. 建议软件库缓存
powershell 复制代码
dnf makecache 
  1. 安装epel-release软件仓
    1. 下载对应版本epel-release的软件仓库
powershell 复制代码
# 不同系统版本需要安装不同的epel-release
wget https://mirrors.aliyun.com/repo/epel-testing.repo
复制代码
2. 重新建立软件库索引
powershell 复制代码
mv epel-testing.repo /etc/yum.repo.d/
dnf clean all 
dnf makecache 
  1. 安装ansible
powershell 复制代码
dnf -y install ansible

等待安装完成即可

开局配置

  1. 常用文件介绍

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

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

  1. 添加主机到ansible
powershell 复制代码
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命令,会发现执行报错
powershell 复制代码
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. 生成密钥
powershell 复制代码
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脚本文件
powershell 复制代码
- hosts: # 主机组
  remote_user: # 用户名
  tasks:
    - name: push ansible key
      authorized_key: user=root key="{{ lookup('file' ,'密钥存放位置')}}" state=present

示例:

powershell 复制代码
- 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脚本文件
powershell 复制代码
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命令
powershell 复制代码
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命令测试是否可以免密登录

powershell 复制代码
ssh root@192.168.0.11

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

相关推荐
---学无止境---21 分钟前
Linux中驱动程序通过fasync异步通知应用程序的实现
linux
cccyi725 分钟前
Linux 进程间通信机制详解
linux·进程通信
北京迅为29 分钟前
【北京迅为】iTOP-4412精英版使用手册-第三十五章 WEB控制LED
linux·嵌入式硬件·嵌入式·4412
让我们一起加油好吗43 分钟前
【C++】封装红黑树模拟实现 set 和 map
linux·c++·set·map·红黑树
暴富奥利奥1 小时前
完成docker方式的ros环境配置
linux·学习·docker·容器
秃头菜狗1 小时前
十四、运行经典案例 wordcount
大数据·linux·hadoop
ManageEngineITSM1 小时前
IT 服务自动化的时代:让效率与体验共进
运维·数据库·人工智能·自动化·itsm·工单系统
QotomPC2 小时前
软件定义的理想硬件平台:Qotom Q30900SE/UE系列在AIO服务器与边缘网关中的实践
运维·服务器
望获linux2 小时前
【实时Linux实战系列】实时系统的可观测性:Prometheus 与 Grafana 集成
大数据·linux·服务器·开发语言·网络·操作系统
捷智算云服务2 小时前
H200服务器维修服务体系构建:捷智算的全链条保障方案
运维·服务器