• Ansible批量添加主机


    一、管理端生成RSA公钥

    ssh-keygen -t rsa
    

    二、单台添加目标主机

    ssh-copy-id root@10.0.0.21
    输入密码后免密连接建立
    

    三、批量添加目标主机

    1.在/etc/ansible/hosts中编辑要批量添加的主机组

    [GROUP-CC]
     aaa ansible_connection=ssh ansible_user=root ansible_ssh_pass="111111"
     bbb ansible_connection=ssh ansible_user=root ansible_ssh_pass="222222"
     ccc ansible_connection=ssh ansible_user=root ansible_ssh_pass="333333"
     ddd ansible_connection=ssh ansible_user=root ansible_ssh_pass="444444"
     eee ansible_connection=ssh ansible_user=root ansible_ssh_pass="555555"
    

    2.编辑推送公钥的yaml文件

    cat /etc/ansible/roles/ssh_key/ssh-key.yaml,为了保证sshd权限统一,可以统一sshd_config配置并重启sshd,sshd_config提前放置在files目录下。

    执行方法 ansible-playbook ssh-key.yaml -e "remote_ip=xx.xx.xx.xx" -k

    - hosts: '{{ remote_ip }}'
      gather_facts: false
      #不收集远程主机信息
      user: root
    
      tasks:
      - name: ssh-copy
        authorized_key:
          user: root
          key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
          #本地读取公钥位置
          path: '/root/.ssh/authorized_keys'
          state: present
          #present添加 absent删除
          exclusive: no
          #是否移除authorized_keys文件中其它非指定key
    
      - name: cpsshd
        copy: src=sshd_config dest=/etc/ssh/sshd_config
    
      - name: Restart the sshd service
        service:
          name: sshd
          state: restarted
    

      

    3.测试并完成推送

    ansible-playbook -C /etc/ansible/roles/ssh_key/ssh-key.yaml
    ansible-playbook  /etc/ansible/roles/ssh_key/ssh-key.yaml  

      

     四、常见问题处理

    1、权限问题 

    192.168.200.111 | UNREACHABLE! => {
        "changed": false, 
        "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
    ", 
        "unreachable": true
    }

      出现上面问题的原因一般为用户ssh权限问题,需检查ansible使用的连接用户是否有ssh的权限

      root用户时,需要目标机sshd_config开启如下配置

    PermitRootLogin yes
    StrictModes no

    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
    sed -i 's/#StrictModes yes/StrictModes no/g' /etc/ssh/sshd_config

      

    2、非root用户,需要在/etc/ansible/hosts或者自定义的inventory.ini中指定用户ansible_user=XXX,

    [GROUP-CC]
     aaa ansible_connection=ssh ansible_user=root ansible_ssh_pass="111111"
     bbb ansible_connection=ssh ansible_user=root ansible_ssh_pass="222222"

           自定义该用户公钥并发送

    ssh-keygen -t rsa #注意第一步时需要指定路径和钥匙名称 /home/aaa/.ssh/ansibleaaa
    ssh-copy-id -i /home/aaa/.ssh/ansibleaaa aaa

      指定公钥

    ansible aaa -m ping --private-key=/home/aaa/.ssh/ansibleaaa

      可以根据需要在管理端的~/.ssh/中添加一个映射用的config文件

      cat ~/.ssh/config

    #Host    hostname
    #user    username
    Host    host1
    user    user1
    Host    host2
    user    user2
    #在ssh时,系统会根据Host的映射关系选择连接的用户,只要用ssh连接host2,就会默认用user2用户连接

      

  • 相关阅读:
    反馈更多的信息,让别人感到舒服。
    Centos 安装Redis
    CentOS用yum安装MySQL 8.0 .
    MySQL事务。
    Java垃圾回收。
    类加载机制与类加载器。
    Java内存模型。
    844--Backspace String Compare
    maven的配置及基本操作
    idea基本使用
  • 原文地址:https://www.cnblogs.com/suminem/p/13603224.html
Copyright © 2020-2023  润新知