ansible软件安装部署

本文介绍如何使用Ansible进行自动化环境架构规划与部署,包括基于SSH密钥的远程连接建立、免交互批量分发公钥的方法及脚本实现,以及Ansible在管理主机和受控主机上的安装配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

a、 ansible软件自动化环境架构规划(服务端:yum install -y ansible 客户端:无需任何安装操作)

  管理主机1台:
  10.0.0.61   m01
  受控主机3台:
  10.0.0.41   backup
  10.0.0.31   nfs01
  10.0.0.7    web01
  Linux系统 6.9

b、ansible软件自动化部署条件

  建议基于ssh密钥方式建立远程连接
  a ssh密钥对创建(管理主机)
    ssh-keygen -t dsa
    影响免交互创建密钥对创建因素:
    1)需要指定私钥存放路径
       -f /root/.ssh/id_dsa
    2)需要进行私钥文件密码设定
       -N/-P  
       -N ""/-P ""

    免交互创建密钥对方法
    ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""

  b 分发公钥文件(管理主机进行分发)
    ssh-copy-id -i /root/.ssh/id_dsa.pub 172.16.1.31
    注:当端口不是22时, ssh-copy-id -i /root/.ssh/id_dsa.pub "172.16.1.31  -p52113"
    影响免交互批量分发密钥因素
    1)需要有确认连接过程,需要输入yes/no
       -o StrictHostKeyChecking=no(不要再进行询问我了)
       sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no 172.16.1.31"

    2)需要解决密码问题
       sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub 172.16.1.31     注意:sshpass需要yum安装
       Now try logging into the machine, with "ssh '172.16.1.31'", and check in:

         .ssh/authorized_keys

       to make sure we haven't added extra keys that you weren't expecting.

    免交互批量分发公钥脚本:
    #!/bin/bash
    rm /root/.ssh/id_dsa
    ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""

    for ip in 31 41 7
    do
    sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no 172.16.1.$ip"
    done        
c 检查是否可以进行基于密钥远程管理
    ssh 172.16.1.31 uptime
    免交互批量检查测试脚本

  基于ssh口令方式建立远程连接(也可以)
  vim /etc/ansible/hosts
  [oldboy]
  172.16.1.7
  172.16.1.31 ansible_user=root ansible_password=123456
  172.16.1.41

  ansible 172.16.1.31 -m command -a "hostname" -k     --- 实现口令交互式远程管理(这是指在/etc/ansible/hosts文件中没有存密码,用-k来实现密码交互)
  SSH password: 
  172.16.1.31 | SUCCESS | rc=0 >>
  nfs01

c ansible软件下载安装

  ansible管理主机软件安装:
  yum install -y ansible
  ansible受控主机软件安装:(可选)
  yum install -y libselinux-python 
    注:安装了libselinux-python软件,即使客户端开启selinux,也不会影响使用

d ansible软件受控主机添加配置

  cat /etc/ansible/hosts
  [oldboy]
  172.16.1.7
  172.16.1.31
  172.16.1.41
  测试方法:
  ansible oldboy -m command -a "hostname"
 172.16.1.31 | SUCCESS | rc=0 >>
  nfs01
 172.16.1.41 | SUCCESS | rc=0 >>
  backup
 172.16.1.7 | SUCCESS | rc=0 >>
  web01

转载于:https://blog.51cto.com/tangyong/2126839

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值