ansible-添加ansible用户

$ vim /etc/ansible/playbooks/ansible_user.yml


---
- name: Playbook for ansible_user
  hosts: '{{hosts_group}}'
  become: yes
  become_user: root
  roles:
    - ansible_user

vim /etc/ansible/playbooks/roles/ansible_user/tasks/main.yml

---

- name: Add ansible_group
  group: name={{ansible_group}} state=present gid={{ansible_groupid}}
  tags:
    - ansible
    - user

- name: Add ansible_user
  user: name={{ansible_user}} state=present createhome=yes shell="/bin/bash" uid={{ansible_userid}} group={{ansible_group}} groups={{ansible_groups}}
  tags:
    - ansible
    - user

- name: Ensure ansible_user .ssh directory exist
  file: path=/home/{{ansible_user}}/.ssh state=directory owner={{ansible_user}} group={{ansible_group}} mode=0700
  tags:
    - ansible
    - user

- name: Add ansible_user ssh_key
  template: src=ansible_authorized_keys.j2 dest=/home/{{ansible_user}}/.ssh/authorized_keys owner={{ansible_user}} group={{ansible_group}} mode=600
  tags:
    - ansible
    - user
    - ssh_key

vim roles/ansible_user/defaults/main.yml

---

ansible_user: "ansible"
ansible_group: "ansible"
ansible_groups: "{% if ansible_os_family == 'RedHat' %}wheel{% elif ansible_os_family == 'Debian' %}sudo{% endif %}"

ansible_userid: 1001
ansible_groupid: 1001

vim roles/ansible_user/templates/ansible_authorized_keys.j2

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4oS7ogPOFuS1YB8iuje6NFyyAtlkP//nYMH38dwogWgOzeRHMVNQCw55ccASsyqnDwsYs3pscEftpEew8LHz0MBm40i8cgqmpOjdEc/4yv0s/YVZ90Sl8CTHAZcis4mohGK9xrEkb0wxvLokAUPp6zB7gQFeWuRoPFADtQXLKtVGoPAWXWiYW/F50OXyP4zmqXHMyIA+a9RZR890BBSjUajGYq7B6Kk1WBRNEKoTB8LdK43LhlT2TODxNJJfMzD1483Pnxb4Qivg1Yc7xDDfItQ0WDw3rAIA6y9kRVj7/EdzonEnHCoXk4L59PGCQGO4SkwlOGigFHzSnDjZAZBfSw== ansible@ansible1.hanli.com

你可能感兴趣的:(linux)