VOLTTRON / volttron-installer

BSD 3-Clause "New" or "Revised" License
0 stars 4 forks source link

Use ansible to grab local ipv4 addresses to determine "local" connection #15

Open craig8 opened 9 months ago

craig8 commented 9 months ago

Local connections should have ansible_connection local and the ip address associated with it.

all:
  hosts:
    substation1:
      ansible_host: 127.0.0.1
      ansible_connection: local
      volttron_home: volttron_home1

This is how a local connection is built within the ansible inventory.yml file. Then running

ansible-playbook -K -i inventory.yml  volttron.deployment.host_config

will install the sudo user requirements for the base system of volttron.

Local host ipv4 addresses.

---
- hosts: localhost
  connection: local
  tasks:
    - debug: var=ansible_all_ipv4_addresses
    - debug: var=ansible_default_ipv4.address

This is one way to get the ipv4 addresses from the system.

~/.volttron_installer/platforms$ ansible-playbook localhost.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ***************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "ansible_all_ipv4_addresses": [
        "172.17.0.1",
        "10.1.0.7",
        "172.18.0.1"
    ]
}

TASK [debug] ***************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "ansible_default_ipv4.address": "10.1.0.7"
}
craig8 commented 9 months ago

You can also use

import socket
socket.gethostbyname(socket.gethostname())

To get an ip address for the current system.