Ansible环境搭建

安装

环境搭建:

  1. 首先准备三台虚拟机一台为受管主机

    三台ip为
    master 192.168.190.132
    node1  192.168.190.133
    node2  192.168.190.133
    
  2. 编辑三个虚拟机的hosts文件

    vim /etc/hosts
    
    #添加如下内容
    192.168.190.132 master
    192.168.190.133 node1
    192.168.190.133 node2
    

    在添加结束之后可以通过ping命令去测试

  3. 在所有的主机上添加用户

    useradd xiaoming
    # 设置密码
    echo 123 | passwd --stdin xiaoming
    
    #### 在master主机上添加admin账号
    useradd admin
    echo 123 | passwd --stdin admin
    
  4. 通过ssh服务设置免密登录

    1. 通过ssh-keygen生成公私钥
      在这里插入图片描述

    2. 将生成的公钥文件传给master的小明用户
      在这里插入图片描述
      验证是否登录成功

      [admin@master .ssh]$ ssh xiaoming@master
      [xiaoming@master ~]$ 
      
      # 两次操作的命令  
      ssh-keygen
      ssh-copy-id xiaoming@master
      
    3. 之后配置免密登录node1和node2上的xiaoming用户

      [xiaoming@master ~]$ exit
      logout
      Connection to master closed.
      [admin@master .ssh]$ ssh-copy-id xiaoming@node1
      /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/admin/.ssh/id_rsa.pub"
      The authenticity of host 'node1 (192.168.190.133)' can't be established.
      ECDSA key fingerprint is SHA256:I4i9y0tllW54OmDyetLIB9avfFogySfv7Ldjx4a3kNk.
      ECDSA key fingerprint is MD5:5a:1c:2c:95:28:b1:b2:4e:bb:56:07:bb:7a:84:5e:83.
      Are you sure you want to continue connecting (yes/no)? yes
      /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
      /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
      xiaoming@node1's password: 
      
      Number of key(s) added: 1
      
      Now try logging into the machine, with:   "ssh 'xiaoming@node1'"
      and check to make sure that only the key(s) you wanted were added.
      
      [admin@master .ssh]$ ssh-copy-id xiaoming@node2
      /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/admin/.ssh/id_rsa.pub"
      The authenticity of host 'node2 (192.168.190.134)' can't be established.
      ECDSA key fingerprint is SHA256:I4i9y0tllW54OmDyetLIB9avfFogySfv7Ldjx4a3kNk.
      ECDSA key fingerprint is MD5:5a:1c:2c:95:28:b1:b2:4e:bb:56:07:bb:7a:84:5e:83.
      Are you sure you want to continue connecting (yes/no)? yes
      /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
      /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
      xiaoming@node2's password: 
      
      Number of key(s) added: 1
      
      Now try logging into the machine, with:   "ssh 'xiaoming@node2'"
      and check to make sure that only the key(s) you wanted were added.
      
      

      验证:

      [admin@master .ssh]$ ssh xiaoming@node1
      [xiaoming@node1 ~]$ exit
      logout
      Connection to node1 closed.
      [admin@master .ssh]$ 
      
  5. 使用admin在master上安装python36和ansible

    #添加扩展源,安装ansible
    vim /etc/yum.repos.d/epel.repo
    
    #添加如下内容
    [epel]
    name=epel
    baseurl=https://mirrors.aliyun.com/epel-archive/7.8/x86_64/
    gpgcheck=0
    

    在master主机上安装ansible

    sudo yum install -y ansible
    
  6. 查看是否安装成功

    ansible: error: too few arguments
    [admin@master yum.repos.d]$ ansible --version
    ansible 2.9.13
      config file = /etc/ansible/ansible.cfg
      configured module search path = [u'/home/admin/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/lib/python2.7/site-packages/ansible
      executable location = /usr/bin/ansible
      python version = 2.7.5 (default, Jun 28 2022, 15:30:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
    

Ansible的配置文件的清单文件

对于ansible的配置问价读取顺序是:当前的工作目录,若没有,则去当前用户的家目录,若依旧没有,则去/etc/ansible/ansible.cfg。