7

I am trying to run a simple ansible operation which should update a line in /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*$"

When I run this in virtualbox with ubuntu 16, it works fine.

When I run it in my ubuntu 16 Docker container, I get:

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"}

Where is ansible trying to pull the local ip from and why can't it do so in docker?

BTW I have installed net-tools in my docker container and it has an eth0 ip.

On virtualbox and in docker I have a line in /etc/hosts

ansible-master 127.0.1.1

UPDATE:

I run

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

on virtualbox ubuntu and Docker ubuntu.

On Virtualbox I get a lot of network-related info that I don't get on Docker:

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

Also in virtualbox I get

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

None of this appears in Docker.

nettie
  • 628
  • 1
  • 11
  • 23
  • When I do ansible all -m setup --tree /tmp/facts I get "msg": "Failed to connect to the host via ssh: ssh: connect to host ansible-master port 22: Connection refused\r\n", so this is making me think that I need ssh to be installed. – nettie Jan 04 '17 at 17:49
  • Add [`connection: local`](https://docs.ansible.com/ansible/playbooks_delegation.html#local-playbooks) – techraf Jan 04 '17 at 18:04
  • Any help would be very much appreciated. The question is how to set up Docker so that Ansible can find `ansible_default_ipv4`. Thank you!! – nettie Jan 04 '17 at 20:07

3 Answers3

12

I have had a similar problem with fedora; the solution was to install the package that provides the 'ip' command (which is used to generate the fact your looking for). in the case of fedora 'dnf install iproute'.

kilroy396
  • 121
  • 1
  • 4
8

For Ubuntu, you have to install the iproute2 package in your pre_tasks. Don't forget to gather facts again in another task with - setup: afterwards.

SuperSandro2000
  • 581
  • 10
  • 20
springloaded
  • 1,079
  • 2
  • 13
  • 23
2

Use hostname flag to put your local container hostname in /etc/hosts:

docker run --hostname=my_hostname

nettie
  • 628
  • 1
  • 11
  • 23