美文网首页ansible
ansible 创建用户,并配置密钥(key)

ansible 创建用户,并配置密钥(key)

作者: 偷油考拉 | 来源:发表于2022-01-27 14:36 被阅读0次

    https://docs.ansible.com/ansible/2.9/modules/authorized_key_module.html

    • Adds or removes SSH authorized keys for particular user accounts
    ---
     - hosts: all
       vars:
         - devops_password: 'abcddefsfdfdfdfdfdfdfdfdfdfd'
       gather_facts: no
       remote_user: ubuntu
       become: true
    tasks:
    - name: Add a new user named devops
         user:
              name: devops
              shell: /bin/bash
              password: "{{ devops_password }}"
    - name: Add devops user to the sudoers
         copy:
              dest: "/etc/sudoers.d/devops"
              content: "devops  ALL=(ALL)  NOPASSWD: ALL"
    - name: Deploy SSH Key
         authorized_key: user=devops
                         key="{{ lookup('file', '/home/devops/.ssh/id_rsa.pub') }}"
                         state=present
    - name: Disable Password Authentication
         lineinfile:
               dest=/etc/ssh/sshd_config
               regexp='^PasswordAuthentication'
               line="PasswordAuthentication no"
               state=present
               backup=yes
    - name: Disable Root Login
         lineinfile:
               dest=/etc/ssh/sshd_config
               regexp='^PermitRootLogin'
               line="PermitRootLogin no"
               state=present
               backup=yes
         notify:
           - restart ssh
    handlers:
       - name: restart ssh
         service:
           name=sshd
           state=restarted
    

    相关文章

      网友评论

        本文标题:ansible 创建用户,并配置密钥(key)

        本文链接:https://www.haomeiwen.com/subject/zvrgzltx.html