Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible_default_ipv4.address undefined in docker ubuntu

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.

like image 526
nettie Avatar asked Jan 04 '17 17:01

nettie


People also ask

What is default IPv4 address in Ansible?

ansible_default_ipv4.address|default (ansible_all_ipv4_addresses) We use default_ipv4.address if it’s available and falling to ‘fist available IP’ if it’s not. ansible_all_ipv4_addresses contains all server IP addresses except for localhost. This is example of facts for server with no default IPv4 address:

How do I set up Ansible on Ubuntu?

To set this up, you can follow Step 2 of How to Set Up SSH Keys on Ubuntu 20.04. To begin using Ansible as a means of managing your server infrastructure, you need to install the Ansible software on the machine that will serve as the Ansible control node. We’ll use the default Ubuntu repositories for that.

What is an ansible host?

One or more Ansible Hosts: An Ansible host is any machine that your Ansible control node is configured to automate. This guide assumes your Ansible hosts are remote Ubuntu 20.04 servers. Make sure each Ansible host has: The Ansible control node’s SSH public key added to the authorized_keys of a system user.

How do I run ad-hoc commands in Ansible?

After confirming that your Ansible control node is able to communicate with your hosts, you can start running ad-hoc commands and playbooks on your servers. Any command that you would normally execute on a remote server over SSH can be run with Ansible on the servers specified in your inventory file.


3 Answers

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'.

like image 167
kilroy396 Avatar answered Oct 01 '22 19:10

kilroy396


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.

like image 37
springloaded Avatar answered Oct 01 '22 19:10

springloaded


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

docker run --hostname=my_hostname

like image 35
nettie Avatar answered Oct 01 '22 20:10

nettie