From 0c2dbd706372a2b6995d2b88dd4755d886a7bfaf Mon Sep 17 00:00:00 2001 From: "Glenn K. Lockwood" Date: Mon, 27 Jul 2020 21:30:58 -0700 Subject: [PATCH] update to work in local mode --- roles/common/tasks/loadconfig.yml | 39 +++++++++++++++++++++++++++ roles/common/tasks/main.yml | 45 +------------------------------ 2 files changed, 40 insertions(+), 44 deletions(-) create mode 100644 roles/common/tasks/loadconfig.yml diff --git a/roles/common/tasks/loadconfig.yml b/roles/common/tasks/loadconfig.yml new file mode 100644 index 0000000..da0cac2 --- /dev/null +++ b/roles/common/tasks/loadconfig.yml @@ -0,0 +1,39 @@ +--- +# Self identify and load config - this allows the playbook to run on either +# localhost or a remote host +- name: store MAC address for eth0 + set_fact: + my_macaddr: "{{ hostvars[inventory_hostname].ansible_eth0.macaddress }}" + when: inventory_hostname == 'localhost' + tags: + - raspi + - sw + - sshd + - motd + +- name: load raspberry pi configuration (local mode) + include_vars: + file: "../../../host_vars/{{ macaddrs[my_macaddr] }}" + name: myconfig + when: inventory_hostname_short == 'localhost' + tags: + - raspi + - sw + - sshd + - motd + +- name: load raspberry pi configuration (remote mode) + set_fact: + myconfig: "{{ hostvars[inventory_hostname_short] }}" + when: "inventory_hostname_short != 'localhost'" + tags: + - raspi + - sw + - sshd + - motd + +- debug: + msg: "loaded configuration for {{ myconfig.hostname }}" + +- assert: + that: "{{ myconfig|length > 0 }}" diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml index a07490b..a3d2c7c 100644 --- a/roles/common/tasks/main.yml +++ b/roles/common/tasks/main.yml @@ -1,48 +1,5 @@ --- - -# Self identify and load config - this allows the playbook to run on either -# localhost or a remote host -- name: store MAC address for eth0 - set_fact: - my_macaddr: "{{ hostvars[inventory_hostname].ansible_eth0.macaddress }}" - when: inventory_hostname == 'localhost' - tags: - - raspi - - sw - - sshd - - motd - -- name: self-identify based on mac address (local mode) - set_fact: - myhostname: "{{ macaddrs[my_macaddr] }}" - when: inventory_hostname_short == 'localhost' - tags: - - raspi - - sw - - sshd - - motd - -- name: self-identify based on mac address (remote mode) - set_fact: - myhostname: "{{ inventory_hostname_short }}" - when: "inventory_hostname_short != 'localhost'" - tags: - - raspi - - sw - - sshd - - motd - -- name: store system configuration - set_fact: - myconfig: "{{ hostvars[myhostname] }}" - tags: - - raspi - - sw - - sshd - - motd - -- debug: - var: myconfig +- include: loadconfig.yml # Gather facts specific to the Raspberry Pi platform - include: raspi-facts.yml