summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorximon18 <3304436+ximon18@users.noreply.github.com>2020-06-17 05:07:52 +0200
committerGitHub <noreply@github.com>2020-06-16 20:07:52 -0700
commit0e9477262f28022bc30985f23b0b34c9f154943d (patch)
treea3050c058de903043297c7d2db564e28951c6d40
parenteb2fae7951a9afe8fdb704b7ae24d31020b1bb7f (diff)
downloadansible-0e9477262f28022bc30985f23b0b34c9f154943d.tar.gz
Add validity check for [Driver][IPAddress] else use docker-machine ip command. (#69696)
* Add validity check for [Driver][IPAddress] else use docker-machine ip command. (backport of https://github.com/ansible-collections/community.general/pull/412) * Add validity check for [Driver][IPAddress] else use docker-machine ip command. * Note why we fallback to the `docker-machine ip <machine name>` command. Co-authored-by: Ben Roose <ben.roose@wichita.edu> * Backport the changelog as requested. Co-authored-by: Ben Roose <ben.roose@wichita.edu>
-rw-r--r--changelogs/fragments/412-docker-machine-add-ip-fallback.yaml2
-rw-r--r--lib/ansible/plugins/inventory/docker_machine.py18
2 files changed, 19 insertions, 1 deletions
diff --git a/changelogs/fragments/412-docker-machine-add-ip-fallback.yaml b/changelogs/fragments/412-docker-machine-add-ip-fallback.yaml
new file mode 100644
index 0000000000..b8ea16d158
--- /dev/null
+++ b/changelogs/fragments/412-docker-machine-add-ip-fallback.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+ - docker_machine - fallback to ip subcommand output if IPAddress is missing (https://github.com/ansible-collections/community.general/issues/412). \ No newline at end of file
diff --git a/lib/ansible/plugins/inventory/docker_machine.py b/lib/ansible/plugins/inventory/docker_machine.py
index 4b6007868e..50b364e50b 100644
--- a/lib/ansible/plugins/inventory/docker_machine.py
+++ b/lib/ansible/plugins/inventory/docker_machine.py
@@ -176,6 +176,14 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
return json.loads(inspect_lines)
+ def _ip_addr_docker_machine_host(self, node):
+ try:
+ ip_addr = self._run_command(['ip', self.node])
+ except subprocess.CalledProcessError:
+ return None
+
+ return ip_addr
+
def _should_skip_host(self, machine_name, env_var_tuples, daemon_env):
if not env_var_tuples:
warning_prefix = 'Unable to fetch Docker daemon env vars from Docker Machine for host {0}'.format(machine_name)
@@ -210,9 +218,17 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
# add an entry in the inventory for this host
self.inventory.add_host(machine_name)
+ # check for valid ip address from inspect output, else explicitly use ip command to find host ip address
+ # this works around an issue seen with Google Compute Platform where the IP address was not available
+ # via the 'inspect' subcommand but was via the 'ip' subcomannd.
+ if self.node_attrs['Driver']['IPAddress']:
+ ip_addr = self.node_attrs['Driver']['IPAddress']
+ else:
+ ip_addr = self._ip_addr_docker_machine_host(self.node)
+
# set standard Ansible remote host connection settings to details captured from `docker-machine`
# see: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
- self.inventory.set_variable(machine_name, 'ansible_host', self.node_attrs['Driver']['IPAddress'])
+ self.inventory.set_variable(machine_name, 'ansible_host', ip_addr)
self.inventory.set_variable(machine_name, 'ansible_port', self.node_attrs['Driver']['SSHPort'])
self.inventory.set_variable(machine_name, 'ansible_user', self.node_attrs['Driver']['SSHUser'])
self.inventory.set_variable(machine_name, 'ansible_ssh_private_key_file', self.node_attrs['Driver']['SSHKeyPath'])