ansible_default_ipv4.address在docker ubuntu中未定义

我正在尝试运行一个简单的ansible操作,它应该更新/ etc / hosts中的一行:

- hosts: localhost
  become: true
  vars:
      master_host: "ansible-master"
  tasks:
  - hostname: name="{{master_host}}"
  - name: Add master host to /etc/hosts
    lineinfile: dest=/etc/hosts line="{{ ansible_default_ipv4.address}} {{master_host}}"
                regexp=".*{{master_host}}\s*$"

当我使用ubuntu 16在virtualbox中运行它时,它工作正常.

当我在我的ubuntu 16 Docker容器中运行它时,我得到:

fatal: [localhost]: FAILED! => {“failed”: true, “msg”: “the field
‘args’ has an invalid value, which appears to include a variable that
is undefined. The error was: ‘ansible_default_ipv4’ is
undefined\n\nThe error appears to have been in
‘/home/user/ansible/manage-ansible-master.yml’: line 11, column 5, but
may\nbe elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n – hostname:
name=\”{{master_host}}\”\n – name: Add master host to /etc/hosts\n
^ here\n”}

ansible试图从本地ip中取出它为什么不能在docker中这样做?

顺便说一句,我在我的docker容器中安装了net-tools,它有一个eth0 ip.

在virtualbox和docker中,我在/ etc / hosts中有一行

ansible-master 127.0.1.1

更新:

我跑

ansible all --connection=local -m setup | less

在virtualbox ubuntu和Docker ubuntu上.

在Virtualbox上,我得到了许多与Docker无关的网络相关信息:

"ansible_facts": {
        "ansible_all_ipv4_addresses": [
            <ip>, 
            <another ip>
        ], 
        "ansible_all_ipv6_addresses": [
            <ipv6>, 
            <another ipv6>
        ], 

我也得到了虚拟机

 "ansible_default_ipv4": {
            "address": <value>, 
            "alias": <value>, 
            "broadcast": <value>, 
            "gateway": <value>, 
            "interface": <value>, 
            "macaddress": <value>, 
            "mtu": <value>, 
            "netmask": <value>, 
            "network": <value>, 
            "type": <value>
        }, 

这些都不会出现在Docker中.

最佳答案
我和fedora有类似的问题;解决方案是安装提供’ip’命令的包(用于生成您要查找的事实).在fedora’dnf安装iproute’的情况下.

转载注明原文:ansible_default_ipv4.address在docker ubuntu中未定义 - 代码日志