【问题标题】:Run ad hoc Ansible commands in Vagrant?在 Vagrant 中运行临时 Ansible 命令?
【发布时间】:2013-10-11 22:18:31
【问题描述】:

在构建 Vagrant 项目时,运行临时 Ansible 任务而不是向 playbook 添加测试命令会很有帮助。我尝试了几种针对 VM 的方法,但一直收到此错误:

default | FAILED => SSH encountered an unknown error during the connection. We 
recommend you re-run the command using -vvvv, which will enable SSH debugging
output to help diagnose the issue

我正在使用 Vagrant 生成的 Ansible 清单文件,并且该框有一个有效的主机名。如何使用单个 Ansible 任务来定位我的 Vagrant VM?

【问题讨论】:

  • 对于为什么有人可能想要这样做的好奇......到目前为止,还没有适用于 Windows 的 Ansible 控制机器。碰巧的是,如果您想学习 Ansible,创建 Vagrant 工作站(以及您想测试控制的其他机器)似乎是一种安全的学习方式。

标签: vagrant ansible ansible-ad-hoc


【解决方案1】:

我丢失了 Vagrant 的私有 ssh 密钥。在这里找到:stackoverflow.com/a/18943360/503463

有几种方法可以做到这一点,但这是我正在使用的:

ansible all -i vagrant_ansible_inventory_default -u vagrant --private-key ~/.vagrant.d/insecure_private_key -m ping

-m 之前的所有内容基本上都是样板文件。我正在使用默认用户名“vagrant”的标准框。 -i vagrant_ansible_inventory_default 标志告诉 Ansible 使用 Vagrant 生成的清单文件;它包含一个主机,因此定位all 是安全的(“默认”也可以)。最后,我们通过 Vagrant 私钥来验证 ssh 连接:--private-key ~/.vagrant.d/insecure_private_key

【讨论】:

  • 另外,您可以使用ansible_ssh_useransible_ssh_private_key_file 参数将用户和私钥直接添加到库存文件中,这样您就不必一直输入这些。见List of Behavioral Inventory Parameters
【解决方案2】:

虽然这个问题很老但是在ansible documentation上已经解释过了

这里是sn-p

ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory --private-key=~/.vagrant.d/insecure_private_key -u vagrant playbook.yml

希望,这将对其他人有所帮助。谢谢

【讨论】:

    【解决方案3】:

    解决方案

    如果您不想一直使用额外的标志,请在与您的 Vagrantfile 相同的目录中创建一个 ansible.cfg,其内容如下:

    [defaults]
    inventory = .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
    

    然后你就可以运行你的临时命令了:

    ansible all -m setup
    

    详情

    第一次使用 Ansible 配置器时,Vagrant 会写出一个清单文件,其中包含所有必需的参数,包括用户名和 ssh 密钥信息。您可以使用-i 标志指定它:

    ansible all -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory \
            -m setup
    

    您也可以使用ANSIBLE_INVENTORY 环境变量:

    export ANSIBLE_INVENTORY=.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
    

    【讨论】:

      【解决方案4】:

      在主机的~/.ssh/known_hosts 文件中删除指向127.0.0.1 的条目。然后再次尝试配置 vagrant 实例。

      【讨论】:

        【解决方案5】:

        您可以将它们放入 ansible 配置文件,而不是每次都传递 inventory_file、private_key 和 ssh_user。在这里查看我更详细的答案:https://stackoverflow.com/a/25316963/502457

        @geerlingguy 正确地指出您可以将该信息放入库存文件中。但是,您仍然需要输入清单路径,或者将其添加到您的 ansible.cfg。

        此外,如果您使用 Vagrant 来配置 EC2 盒子(通过插件),则会自动生成清单文件。您可以对其进行编辑,但下次您配置一些新的 EC2 框时,它会被炸毁。

        【讨论】:

          【解决方案6】:

          如果你在这个错误之前运行你的盒子,你应该执行这个:

          ssh-keygen -f "/home/your_user/.ssh/known_hosts" -R host
          

          你也应该运行:

          ssh-keygen -f "/home/your_user/.ssh/known_hosts" -R 127.0.0.1
          

          然后再试一次。

          【讨论】:

            猜你喜欢
            • 2015-01-26
            • 2015-01-29
            • 2021-12-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多