Skip to content

Commit

Permalink
test ansible
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Jul 26, 2023
1 parent 47c4d7c commit 1a7d7a2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
84 changes: 84 additions & 0 deletions services/ansible_openvpn/openvpn_ansible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# BSD 3-Clause License
#
# Copyright (c) 2023, University Gustave Eiffel
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This script convert OpenVPN status into a dynamic Ansible inventory
# It can use local /var/log/openvpn/openvpn-status.log file
# or OpenVPN telnet management console

def parse_openvpn_status(log_text):
# Split the log text into lines
lines = log_text.strip().split('\n')

# Find the start and end indexes for the client list section
start_index = lines.index('ROUTING TABLE') + 2
end_index = lines.index('GLOBAL STATS', start_index)

# Extract the lines containing client information
client_lines = lines[start_index:end_index]

# Create a dictionary to store the hosts and their attributes
hosts = []
fields = lines[start_index-1].split(",")

for line in client_lines:
# Split the line by commas
parts = line.split(',')
data = {}
for index, field_name in enumerate(fields):
data[field_name] = parts[index]
hosts.append(data)

return hosts


def write_to_ansible_inventory(inventory_text, filename):
with open(filename, 'w') as file:
file.write(inventory_text)


def main():
# Replace this with the path to your openvpn-status.log file
log_file_path = "openvpn-status.log"

with open(log_file_path, 'r') as file:
log_text = file.read()

hosts = parse_openvpn_status(log_text)
inventory_text = "\n".join(["%s ansible_port=5555 ansible_host=%s" %
(host["Virtual Address"],
host["Common Name"]) for host in hosts])

# Replace this with the desired path for the Ansible inventory file
ansible_inventory_file = "inventory.ini"
write_to_ansible_inventory(inventory_text, ansible_inventory_file)


if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions services/ansible_openvpn/playbooks/get_temperature.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- name: Run vcgencmd measure_temp command on the remote host
hosts: all
gather_facts: no
connection: ssh
tasks:
- name: Execute vcgencmd measure_temp
ansible.builtin.command: vcgencmd measure_temp
register: temp_output

- name: Print temperature output
ansible.builtin.debug:
var: temp_output.stdout

0 comments on commit 1a7d7a2

Please sign in to comment.