【发布时间】:2016-12-31 06:21:25
【问题描述】:
我正在使用 vagrant 运行 Xenial 16.04 VM。虚拟机开箱即用,有一个 vagrant 用户,其密码为 vagrant,没有 root 的密码(尽管如果你 ssh 进入 root@ubuntu-vm,Ubuntu 仍然需要密码)
为了模拟生产 VPS,我使用这个 playbook 创建了一个deployer 用户:
- user:
name: deployer
groups: admin
password: "{{ deployer_password }}"
shell: /bin/bash
- name: "read authorized keys from root user"
become_user: vagrant
command: "cat ~/.ssh/authorized_keys"
register: "root_authorized_keys"
- debug: msg="{{root_authorized_keys}}"
- name: "create .ssh dir for deployer"
file: path="/home/deployer/.ssh" state=directory
- name: "copy authorized keys to deployer user"
shell: "echo '{{root_authorized_keys.stdout}}' > /home/deployer/.ssh/authorized_keys"
- name: "chown the authorized_keys file"
file: path="/home/deployer/.ssh" recurse=yes mode=0700 owner="deployer"
当我 ssh 进入 deployer@ubuntu-vm 时,我被要求输入密码,然后我按预期登录了 VM。在我输入deployer 用户的密码后,sudo 和 sudo su 也可以正常工作。
当我尝试通过 Ansible playbook 或 Ansible ad-hoc 命令sudo 时出现问题。
注意我对以下 4 个 Ansible 命令的响应:
$ ansible all -m ping -u vagrant
ubuntu-vm | SUCCESS => {
"changed": false,
"ping": "pong"
}
$ ansible all -m ping -u deployer
ubuntu-vm | SUCCESS => {
"changed": false,
"ping": "pong"
}
$ ansible all -m ping -u vagrant --sudo
ubuntu-vm | SUCCESS => {
"changed": false,
"ping": "pong"
}
$ ansible all -m ping -u deployer --sudo
ubuntu-vm | FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "sudo: a password is required\n",
"module_stdout": "",
"msg": "MODULE FAILURE"
}
我必须使用sudo 来成功使用deployer 的哪些选项?
还要考虑到remote_user 的ubuntu-vm 主机将同时是vagrant 和deployer 用户。如果可能的话,我想在剧本中同时使用两者(例如:我使用vagrant 用户创建deployer 用户,然后我将deployer 用户用于其他游戏) p>
【问题讨论】:
标签: ubuntu ssh vagrant ansible ubuntu-16.04