Molecule: ansible_default_ipv4 not populated when using docker driver

Created on 22 May 2017  路  3Comments  路  Source: ansible-community/molecule

Issue Type

  • Bug report

Molecule and Ansible details

ansible --version
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/etc/ansible/modules/']
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

molecule --version
molecule, version 1.24.0
  • Molecule installation method: pip
  • Ansible installation method: pip

Desired Behaviour

The network variables, e.g. ansible_default_ipv4 should be populated when using the docker driver and molecule test.

Actual Behaviour

I'm getting the following error when trying to test my role (with molecule test), despite gather_facts: true being set in the playbook Molecule is executing:

TASK [agent : Apply OSSEC agent configuration] *********************************
fatal: [agent-ubuntu]: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'ansible_default_ipv4' is undefined"}
fatal: [agent-centos]: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'ansible_default_ipv4' is undefined"}

If I dump the variables out in a debug statement, I can see it's not there. However if I connect to the container and run the same command that Ansible uses to determine the default IPv4 address, I get the expected response:

[xiol@xerxes .../roles/agent]% molecule login --host agent-centos
[root@c9b459cd2a8e /]# ip -4 route get 8.8.8.8
8.8.8.8 via 172.17.0.1 dev eth0  src 172.17.0.2

If I manually run the setup module using the inventory Molecule generates, the ansible_default_ipv4 variable is populated as expected:

[xiol@xerxes .../roles/agent]% ansible agent-centos -i .molecule/ansible_inventory -m setup | grep -A1 ansible_default_ipv4
        "ansible_default_ipv4": {
            "address": "172.17.0.2",

In writing out this issue I've actually discovered that it's possible to use the converge command to re-run against the same containers, which makes it work? It appears that the variables are not being fully populated if you're using the all-in-one testoption.

Edit: Further testing shows that this isn't just a case of molecule create && molecule converge - a second converge is required in order to have these variables populated.

Most helpful comment

Molecule does nothing special. It simply calls ansible. I'm guessing your container does not have the iproute package installed until you converge for the first time.

Ansible requires the iproute package for network facts to be populated.

All 3 comments

Molecule does nothing special. It simply calls ansible. I'm guessing your container does not have the iproute package installed until you converge for the first time.

Ansible requires the iproute package for network facts to be populated.

Yep. I have been an idiot.

Thanks for the pointers and sorry for wasting your time.

Thanks for the pointers and sorry for wasting your time.

All good. I stumbled on this issue for many months with my etcd role. Did some really stupid stuff until I read the code, and realized what was going on 馃幐

Was this page helpful?
0 / 5 - 0 ratings