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
网友评论