特别感谢 GROG,他帮助我了解我做错了什么。
root
基本上,我在以非 root 用户身份运行 Ansible playbook 时尝试完成工作。我最终创建了以下内容bootstrap.yml
并使用以下命令运行它:
ansible-playbook ./bootstrap.yml -u root -k
这将使用 root 密码提示以 root 身份运行我的 playbook,并且能够创建用户并建立 sudo 和无密码访问
---
# file: bootstrap.yml
# Execute once as root user to create a public key and install it to your client machine(s) using the following command
# ansible-playbook ./auth-client.yml -u root -k
# This requires you to install GROG.management-user role from the Ansible Galaxy using this command:
# ansible-galaxy install GROG.management-user
# Add pdo user on remote machines
- hosts: all
tasks:
- name: Add remote users
user: name=pdo group=users
# Generate SSK keys at the localhost for pde user
- hosts: localhost
tasks:
- name: Provision local pdo user
user: name=pdo generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa
# Install public key into remote machine
- hosts: all
vars:
authorized_key_list:
- name: pdo
authorized_keys:
- key: "{{ lookup('file', '/home/pdo/.ssh/id_rsa.pub') }}"
state: present
roles:
- { role: GROG.authorized-key }
# Add sudo privileges for pdo user
- hosts: all
roles:
- { role: GROG.sudo, become: yes }