2016-12-30 165 views
-1

我使用vagrant運行Xenial 16.04 VM。虛擬機開箱即用vagrant用戶使用vagrant密碼,但沒有密碼root(儘管如果您將ssh插入root @ ubuntu-vm,Ubuntu仍然需要密碼) 爲了模擬生產VPS,我創建了一個deployer用戶使用這個劇本:不能使用sudo與新創建的Ubuntu用戶通過ansible

- 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到[email protected]有人問我鍵入密碼,然後我記錄在VM作爲一個應該期望。在輸入deployer用戶的密碼後,sudosudo su也按預期工作。

當我嘗試通過Ansible手冊或通過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" 
} 

我有什麼選擇使用sudodeployer成功?

還要考慮到的是,remote_userubuntu-vm主機將是既vagrantdeployer用戶。如果可能的話,我想辦法在劇本同時使用(例如:我用的是vagrant用戶創造deployer用戶,然後我用deployer用戶對其他戲劇)

回答

2

嘗試--ask-sudo-pass

它會提示輸入密碼。

ansible all -m ping -u deployer --sudo --ask-sudo-pass 

在Ansible 1.9+,建議使用--become--ask-become-pass因爲--sudo--ask-sudo-pass已被棄用。 Become (Privilege Escalation)

+0

這可以和'ansible-playbook'一起使用嗎?如果是這樣,我怎麼能通過密碼爲多個用戶? (請參閱我在上面的問題之後編寫的斜體文本) –

+0

如果我只需要爲一個用戶傳遞密碼,但是我無法弄清楚如何爲多個用戶傳遞密碼,則「--ask-become-pass」 。 –

+0

除非您使用Ansible保險庫,否則我認爲不可能爲多個用戶傳遞密碼。請參閱:http://serverfault.com/questions/560106/how-can-i-implement-ansible-with-per-host-passwords-securely – helloV

相關問題