Ansible中缺少sudo密码

时间:2014-08-30 13:11:58

标签: ansible

Ansible从以下代码请求sudo密码,它尝试创建一个新的postgres用户。

错误讯息:

  

致命:[xxx.xxx.xxx.xxx] =>缺少sudo密码

main.yml

- name: 'Provision a PostgreSQL server'
  hosts: "dbservers"
  sudo: yes
  sudo_user: postgres
  roles:
    - postgres

create_db.yml

- name: Make sure the PostgreSQL users are present
  postgresql_user: name=rails password=secret role_attr_flags=CREATEDB,NOSUPERUSER
  sudo_user: postgres
  sudo: yes

用于登录此计算机的remote_user是非root 用户,没有密码,只能使用密钥身份验证登录。

对于用户 postgres ,此帐户也没有密码,因为刚刚安装了数据库。

由于我以 -root用户身份登录,因此当切换到 postgress 帐户以创建数据库用户时,它会询问密码。但是,如果从root帐户切换到 postgres ,则不需要密码。所以,我想知道是否有办法切换到root,然后切换到用户 postgres

注意:root帐户没有公钥,没有密码,也无法从SSH登录。

14 个答案:

答案 0 :(得分:14)

您可以在运行Ansible剧本时指定sudo密码:

ansible-playbook playbook.yml -i inventory.ini --extra-vars "ansible_sudo_pass=yourPassword"

答案 1 :(得分:3)

在我的情况下,我将信息添加到服务器组的组变量中

因此在/ etc / ansible / group_vars / {servergroup} / vars

我添加了

ansible_become: yes 
ansible_become_method: sudo
ansible_become_pass: "{{ vault_ansible_password }}"

本文帮助我锻炼了答案https://www.cyberciti.biz/faq/how-to-set-and-use-sudo-password-for-ansible-vault/

答案 2 :(得分:2)

尝试:

我有类似的问题,我以前用-k -K(大写)运行我的剧本。该剧本将提示您输入密码。

ansible-playbook mail.yml -k -K 

希望有帮助!

答案 3 :(得分:1)

如果您选择“启用特权升级”选项,这将在Ansible Tower UI中发生。您可能需要在Ansible Tower中提供两次密码。

答案 4 :(得分:1)

将此添加到您的/ etc / sudoers文件

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL
username-u-want-to-allow        ALL=(ALL)       NOPASSWD: ALL

答案 5 :(得分:1)

如果上述所有解决方案均不适用于您,那么我就是这种情况。我的问题是ansible_user并没有所有权限,我不想让root从ssh连接。

但是我的测试者用户不具备执行某些操作的所有sudo权限: 初始tester_user权限:

tester ALL= NOPASSWD:ALL     # bad

更改为:

tester ALL=(ALL:ALL) NOPASSWD:ALL    # good

这些附加字段的含义是: 第一个“ ALL”表示用户可以以所有用户身份运行命令。 第二个“ ALL”表示用户可以将命令作为所有组运行。

最初希望限制维护者的权限,但是必须强制ansible_user运行命令,因为所有用户都在Ansible中使用turn_user。

答案 6 :(得分:1)

在您的远程服务器(客户端-服务器)或(目标-服务器)中,无论您调用什么,以 root 用户身份编写此命令

<块引用>

visudo pressenter 下

用户权限说明

<您在(客户端-服务器)上的姓名> ALL=(ALL) NOPASSWD: ALL 保存存档 现在从您的控制器服务器(工作站)或(Ansible-Server)无论您调用什么,运行您的命令

<块引用>

ssh @ipaddress 成功

答案 7 :(得分:0)

您需要修改/etc/sudoers文件,以允许连接到删除服务器的用户切换到另一个没有密码提示的用户。

答案 8 :(得分:0)

如果用于建立连接的ssh_user属于sudoers组,则不需要指定 sudo_user ,只需说 sudo_pass

答案 9 :(得分:0)

使用以下内容将文件添加到名为postgres的目标计算机上的/etc/sudoers.d目录中:

postgres ALL=(ALL) NOPASSWD:ALL

这可以确保postgres用户(假设您将其用作sudo用户)在尝试sudo命令时不会被要求输入密码。

如果您使用其他用户连接目标计算机,则必须修改上述内容,以便为该用户授予NOPASSWD权限。

有关详细信息,请参阅here

答案 10 :(得分:0)

我的解决方案/错误消息的解决方法: 致命:[节点]:失败! => {“ msg”:“缺少sudo密码”}

对我来说,尽管用户已经存在于远程主机上的sudoers文件中,无需使用密码即可执行命令,但我仍然收到此消息。我在YAML主要游戏手册中输入的内容如下:

---

- hosts: [your targeted inventory list of hosts]
  become_user: [your remote privileged user]
  become: true
  roles:
  - [your playbook role]

还在/etc/ansible/ansible.cfg中,我启用/注释或更改了以下内容:

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

[defaults]
remote_tmp = /tmp/ansible-$USER
host_key_checking = False
sudo_user      = [your remote privileged user]
ask_sudo_pass = False
ask_pass      = False

条目 remote_tmp = / tmp / ansible- $ USER 是为了避免显示诸如以下消息:

OSError: [Errno 13] Permission denied: '/etc/.ansible_tmpLyoZOOyum.conf'
fatal: [node]: FAILED! => {"changed": false, "msg": "The destination directory (/etc) is not writable by the current user. Error was: [Errno 13] Permission denied: '/etc/.ansible_tmpLyoZOOyum.conf'"}

答案 11 :(得分:0)

就我而言,我通过在 /bin/sh 行中添加命令 /etc/sudoers 以允许在没有密码的情况下执行命令来解决它。

这是显示的错误:

BECOME password: 
debian | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "module_stderr": "Shared connection to debian9 closed.\r\n",
    "module_stdout": "\r\n",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

只添加这个:

user    ALL= NOPASSWD: /usr/bin/id, /usr/bin/whoami, /bin/sh

出于测试目的,我还添加了 idwhoami

答案 12 :(得分:0)

就我而言,即使密码正确,我也会收到此错误,因为 playbook 指定了“连接:本地”。该剧本将连接类型设置为本地,因为所有命令都应该在本地主机上运行。添加需要委托给远程主机的新任务后,连接方法仍然设置为本地,导致丢失 sudo 密码错误。通过删除剧本中的“连接:本地”修复了该错误。

答案 13 :(得分:-1)

random ALL =(全部)NOPASSWD:全部 为我工作

相关问题