1

设想:

根据文件的 [clients] 部分hosts执行以下操作:

  1. 检查用户“foo”的 SSH 登录是否失败,如果是
  2. 使用 authorized_key 模块为用户“foo”添加 SSH 密钥
  3. 假设远程机器上已经存在用户“foo”并且已经在本地(ansible)主机上创建了 SSH 公钥

我知道使用 Ansible 命令行的此解决方案,但我希望能够将其放入剧本中。使脚本与用户输入密码(包括 sudo)交互是可以接受的。

现在我想出了如何使用 3-rd 方角色来做我想做的事,GROG.authorized-key但它仍然需要我使用 -K 开关运行剧本。Ansible 中是否有某些东西(在命令行开关旁边)只会在需要时提示输入密码?

- hosts: clients
  vars:
    authorized_key_list:
      - name: pdo
        authorized_keys:
         - key: "{{ lookup('file', '/home/pdo/.ssh/id_rsa.pub') }}"
           state: present
  roles:
    - { role: GROG.authorized-key }
4

2 回答 2

1

我认为根据您的评论,这应该可行:

- hosts: clients
  become: true
  tasks: 
  - name: Add authorized_key to pdo user on the remote client machine(s)
    authorized_key: user=foo key="{{ lookup('file', '/home/pdo/.ssh/id_rsa.pub') }}"

使用 -K 调用它以获得成为密码的问题。这将在远程机器上创建一个 sudo 命令。这就是你需要的,不是吗?

于 2016-04-20T10:35:03.717 回答
0

特别感谢 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 }
于 2016-04-21T17:07:37.100 回答