简体   繁体   English

无法使用Vagrant上的Ansible框启动Docker服务

[英]Cannot start the Docker service using Ansible on Vagrant box

---
- hosts: vagrant
  remote_user: root
  become_method: sudo
  tasks:
        - name: Start service docker, if not running
          service:
               name: docker
               state: started

ERROR: fatal: [192.168.1.16]: FAILED! 错误:致命:[192.168.1.16]:失败! => {"changed": false, "msg": "Unable to start service docker: Failed to start docker.service: Interactive authentication required.\\nSee system logs and 'systemctl status docker.service' for details.\\n"} => {“已更改”:false,“ msg”:“无法启动服务docker:无法启动docker.service:需要交互式身份验证。\\ n有关详细信息,请参见系统日志和'systemctl status docker.service'。\\ n”}

I want to start Docker using Ansible to other host (Vagrant). 我想使用Ansible来启动Docker到其他主机(Vagrant)。 I could ping with Ansible to two hosts. 我可以与Ansible ping通两个主机。 But it won't work with Vagrant host. 但这不适用于Vagrant主机。

Have any idea how to fix this? 有任何想法如何解决这个问题?

Root cause 根本原因

The remote_user setting in playbook ( remote_user: root in the question) is ignored when using Ansible provisioner in Vagrant with default settings. 在Vagrant的Ansible Provisioner中使用默认设置时,将忽略剧本中的remote_user设置( remote_user: root问题中的remote_user: root )。

Explanation 说明

There is an important difference between "regular" variables and connection variables in Ansible with regard to precedence: 在优先级方面,“常规”变量和Ansible中的连接变量之间存在重要区别

Another important thing to consider (for all versions) is that connection variables override config, command line and play/role/task specific options and directives. (对于所有版本)要考虑的另一重要事项是,连接变量将覆盖config,命令行以及播放/角色/任务特定的选项和指令。 For example: 例如:

 ansible -u lola myhost 

This will still connect as ramon because ansible_ssh_user is set to ramon in inventory for myhost. 由于ansible_ssh_user在myhost的清单中设置为ramon,因此仍将作为ramon连接。 For plays/tasks this is also true for remote_user: 对于播放/任务,remote_user也是如此:

 - hosts: myhost tasks: - command: i'll connect as ramon still remote_user: lola 

This is done so host-specific settings can override the general settings. 这样做是为了使特定于主机的设置可以覆盖常规设置。

Since you are using Vagrant, it creates an inventory file in under directory .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory and by default (depends also on box's defults) specifies ansible_user='vagrant' inside, which means the remote_user: root is overridden by that default setting. 由于您使用的是Vagrant,因此会在目录.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory下创建一个清单文件,并且默认情况下(取决于框的分隔符)在其中指定ansible_user='vagrant' ,这意味着remote_user: root被覆盖默认设置。

This is mentioned in Vagrant docs as well: Vagrant文​​档中也提到了这一点:

force_remote_user (boolean) - require Vagrant to set the ansible_ssh_user setting in the generated inventory, or as an extra variable when a static inventory is used. force_remote_user (布尔值)-要求Vagrant在生成的清单中设置ansible_ssh_user设置,或者在使用静态清单时作为额外变量进行设置。 All the Ansible remote_user parameters will then be overridden by the value of config.ssh.username of the Vagrant SSH Settings. 然后,所有“ Ansible remote_user参数都将被“ Vagrant SSH设置”的config.ssh.username值覆盖。

If this option is set to false Vagrant will set the Vagrant SSH username as a default Ansible remote user, but remote_user parameters of your Ansible plays or tasks will still be taken into account and thus override the Vagrant configuration. 如果将此选项设置为false Vagrant会将Vagrant SSH用户名设置为默认的Ansible远程用户,但仍会考虑Ansible播放或任务的remote_user参数,从而覆盖Vagrant配置。

The default value is true . 默认值为true

Solutions 解决方案

You can: 您可以:

  • modify your play by adding become: true to either whole play, or a single task; 通过添加become: true来修改您的游戏become: true对整个游戏或单个任务为become: true

  • set force_remote_user to false in the Vagrantfile; force_remote_user设置为false

  • set config.ssh.username to root in the Vagrantfile. 设置config.ssh.usernameroot在Vagrantfile。

The first one being the preferable way. 第一种是首选方式。

Interactive authentication required Seems like the user running the sudo command remotely requires a password for the sudo action and as there no terminal to provide it, it fails. 需要交互式身份验证似乎用户远程运行sudo命令需要sudo操作的密码,并且由于没有终端提供该密码,因此失败。

Try to use a remote user having the right to run sudo command without the need to authenticate. 尝试使用有权运行sudo命令而不需要进行身份验证的远程用户。

You could also use "ansible_sudo_pass" to specify the password to provide remotely. 您也可以使用“ ansible_sudo_pass”来指定要远程提供的密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM