• 快速学会ansible的安装


    1、自定义环境

    准备大于等于三台主机

    角色主机名ip地址组名
    控制主机server.example.com192.168.189.129server
    受控主机/被管节点node1.example.com192.168.189.134node1
    受控主机/被管节点node2.example.com192.168.189.135node2

    (1)配置ip

    给三台主机配置静态IP

    nmcli c mo ens160 ipv4.addresses 172.25.250.100/24 ipv4.gateway 192.168.189.2 ipv4.dns 8.8.8.8 ipv4.method manual connection.autoconnect yes
    
    nmcli connection up ens160 # 重启连接
    
    • 1
    • 2
    • 3

    (2)多台主机配置主机名并且确保多台主机能够通过主机名互访

    hostnamectl set-hostname server.example.com
    hostnamectl set-hostname node1.example.com
    hostnamectl set-hostname node2.example.com
    
    • 1
    • 2
    • 3

    在每台主机的/etc/hosts中配置

    192.168.189.129 server.example.com
    192.168.189.134 node1.example.com
    192.168.189.135 node2.example.com
    
    • 1
    • 2
    • 3

    (3) 多台主机通过ssh远程连接实现免密登陆

    生成秘钥

    ssh-keygen -t rsa -P '' -q -f ~/.ssh/id_rsa
    
    • 1

    将公钥钥拷贝到另外两台主机的用户家目录下的/.ssh/authorized_keys 中(没有的话,创建)

    scp /root/.ssh/id_rsa.pub root@node1.example.com:/root/.ssh/authorized_keys 
    
    scp /root/.ssh/id_rsa.pub root@node2.example.com:/root/.ssh/authorized_keys 
    
    • 1
    • 2
    • 3

    (4) 如果是通过普通用户管理受控主机,需要通过sudo提权。

    2、在控制主机上安装ansible

    (1)配置epel扩展源

    release 一般带这个东西指的是软件厂库.repo

    yum install -y https://mirrors.tuna.tsinghua.edu.cn/epel/epel-release-latest-8.noarch.rpm
    
    • 1

    (2)yum install ansible -y

    由于Redhat8版本过高,而ansible-code版本过低,会导致安装错误

    conflicting requests - nothing provides (ansible-core >= 2.12.2 with ansible-code < 2.12.3)
    
    • 1

    以下有两种解决办法:

    一、直接更改软件yum源,此yum源放入/etc/yum.repos.d/下

    CentOS-Stream(ansible-code).repo

    二、使用pip安装

    1、确保pip可用(以下可直接将python3 -m ->pip3)

    python3 -m pip -V
    
    • 1

    如果一切正常,您应该会看到如下内容:

    python3 -m pip -V
    pip 21.0.1 from /usr/lib/python3.9/site-packages/pip (python 3.9) # python 3.8可行
    
    • 1
    • 2

    如果您看到类似以下的错误No module named pip,您需要安装pip在您选择的Python解释器下。这可能意味着安装额外的操作系统包(例如,python3-pip),或者安装最新的pip通过运行以下命令,直接从Python打包中心获得:

    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    python3 get-pip.py --user
    
    • 1
    • 2

    也可直接将pip更新(不建议)

    pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1

    2、安装Ansible

    使用pip在您选择的Python环境中为当前用户安装您选择的Ansible包:

    python3 -m pip install --user ansible -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1

    或者,您可以安装特定版本的ansible-core在这个Python环境中:

    python3 -m pip install --user ansible-core==2.13.2  -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1

    3、升级Ansible

    python3 -m pip install --upgrade --user ansible -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1

    (3)查看是否安装成功#ansible --version

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OfqiYEcU-1659436549873)(C:\Users\26807\AppData\Roaming\Typora\typora-user-images\image-20220802181622197.png)]

    如果这个为3.6,直接卸载3.6,再安装ansible,再安回python36

    简单测试一下ansible可不可用(保证被控主机有ssh,python)

    vim /etc/ansible/hosts
    [node]
    node1.example.com
    node2.example.com
    
    • 1
    • 2
    • 3
    • 4

    在使用ansible node -m ping

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8wFhTNPw-1659436549874)(C:\Users\26807\AppData\Roaming\Typora\typora-user-images\image-20220802182926182.png)]

    有就证明安装成功

  • 相关阅读:
    EN 14353石膏板嵌金属—CE认证
    JAVA8特点
    CSDN21天学习挑战赛——File类、IO流(08)
    【ARM 嵌入式 编译系列 11.3 -- GCC attribute packed noreturn constructor 介绍】
    常见简单的排序算法汇总
    ✨✨使用vue3打造一个el-form表单及高德地图的关联组件实例✨
    【docker】docker容器搭建分布式LNMP,附错误及解决方案
    go的fasthttp学习
    python数据分析可视化大作业——对地铁数据的简单数据分析
    如何一次性批量打印PDF、Word、Excel、PPT和图片 - 文件批量打印工具
  • 原文地址:https://blog.csdn.net/m0_63342921/article/details/126128112