在 CentOS 7.9 上安装和配置 Ansible

本文档将引导您完成在 CentOS 7.9 系统上安装 Ansible、设置 inventory 文件、配置 SSH 无密码登录等步骤。

安装 Ansible

  1. 使用 Yum 安装 Ansible:

    sudo yum install ansible -y
    

配置 Ansible

  1. 创建 Ansible 工作目录:

    mkdir -p ~/ansible
    
  2. 创建 Ansible 配置文件 ansible.cfg​:

    cat > ~/ansible/ansible.cfg << EOF
    [defaults]
    inventory = ./hosts
    remote_user = root
    host_key_checking = False
    EOF
    
  3. 创建 Inventory 文件 hosts​:

    cat > ~/ansible/hosts << EOF
    [all]
    192.168.2.3
    192.168.2.4
    
    [master]
    192.168.2.3
    
    [worker]
    192.168.2.4
    EOF
    

SSH 无密码登录设置

  1. 生成 SSH 密钥:

    ssh-keygen -t rsa -b 2048
    

    按照提示操作,设置密码或保持空白以创建无密码密钥。

  2. 分发 SSH 公钥到所有主机:

    ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub') }}' path=/root/.ssh/authorized_keys manage_dir=no" --ask-pass -c paramiko
    

    输入 SSH 密码以完成分发。

修改 Ansible 全局配置(可选)

  1. 关闭主机密钥检查和设置日志路径:

    sed -i "s@#host_key_checking = False@host_key_checking = False@g" /etc/ansible/ansible.cfg
    sed -i "s@#log_path = /var/log/ansible.log@log_path = /var/log/ansible.log@g" /etc/ansible/ansible.cfg
    
  2. 指定 Ansible 使用的私钥文件:

    sed -i "s@#private_key_file = /path/to/file@private_key_file = /root/.ssh/id_rsa@g" /etc/ansible/ansible.cfg
    

测试 Ansible 配置

  1. 测试 Ansible 与所有主机的连接:

    ansible all -m ping
    

这份文档涵盖了从安装 Ansible 到配置和测试 SSH 连接的基本步骤。请根据实际情况进行调整和优化。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值