Ansible 創建用戶 Playbook 腳本


創建用戶,設置wheel組sudo不需要密碼,然后將用戶添加到wheel組,並將用戶的公鑰傳輸到節點上:

 

---
- name: Linux Create User and Upload User Public keys
  hosts: test
  #remote_user: xxxx
  #sudo: yes
  vars:
      user_1: xiaoxiaoleo
  tasks:
    - name: Make sure we have a 'wheel' group
      group:
        name: wheel
        state: present

    - name: Allow 'wheel' group to have passwordless sudo
      lineinfile:
        dest: /etc/sudoers
        state: present
        regexp: '^%wheel'
        line: '%wheel ALL=(ALL) NOPASSWD: ALL'

    - name: Create user {{ user_1 }}
      user:
        name: "{{ user_1 }}"
        shell: /bin/bash
        groups: wheel
        createhome: yes
        home: /home/{{ user_1 }}
        state: present

    - name: create key directory
      action: file path=/home/{{ user_1 }}/.ssh/ state=directory  owner={{ user_1 }} group={{ user_1 }} mode=0700

    - name: create key file
      action: file path=/home/{{ user_1 }}/.ssh/authorized_keys state=touch  owner={{ user_1 }} group={{ user_1 }} mode=0600
       

    - name: Set authorized key took from file
      authorized_key:
        user: "{{ user_1 }}"
        state: present
        key: "{{ lookup('file', '/tmp/pubkey/id_rsa.pub') }}"


  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM