Skip to content

Commit ca2ae4a

Browse files
committed
- Add ansible-lint config fro remove some warnings
- Add playbook to demonstrate how to use the role - Update default, template, tests, tests_tasks to remove dev_ prefix variables
1 parent c5f1a72 commit ca2ae4a

File tree

7 files changed

+62
-40
lines changed

7 files changed

+62
-40
lines changed

.ansible-lint

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# .ansible-lint
2+
skip_list:
3+
- run-once # Ignorer les avertissements liés à run_once

defaults/main.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
---
22
# defaults file for ansible-role-hosts-aliases
33

4-
# Default empty lists for host information
5-
dev_hostnames: [] # List of hostnames to create aliases for
6-
dev_ips: [] # List of IPs corresponding to hostnames
7-
dev_alias: [] # List of short aliases for each host
8-
dev_vspaths: [] # List of paths to open in VSCode for each host
9-
dev_ansible_users: [] # List of SSH users for each host
10-
114
# File paths
125
bash: ".bashrc" # Bash profile file to modify
13-
bash_hosts_aliases: ".bash_hosts_aliases" # File to store the aliases
6+
bash_hosts_aliases: ".bashrc_hosts_aliases" # File to store the aliases
147

158
# Editor configuration
169
editor_path: "code" # Path to the VSCode executable

meta/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ galaxy_info:
1313
license: MIT
1414
min_ansible_version: "2.12"
1515

16+
platforms:
17+
- name: GenericLinux
18+
versions:
19+
- all
20+
- name: "MacOSX"
21+
versions:
22+
- all
23+
1624
galaxy_tags:
1725
- development
1826
- system

playbook.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# File: playbook.yml
2+
# Description: Playbook to setup hosts aliases
3+
# Author: Lucas Janin
4+
# Version: 1.0
5+
---
6+
- name: Auto setup hosts on Unifi, PiHole and fastroom
7+
hosts: "{{ target_hosts | default('all') }}"
8+
gather_facts: true
9+
become: true
10+
become_method: ansible.builtin.sudo
11+
strategy: linear
12+
13+
tasks:
14+
- name: Display discovered host information individually
15+
ansible.builtin.debug:
16+
msg:
17+
- "Host: {{ item }}"
18+
- " IP : {{ hostvars[item].ansible_host | default('') }}"
19+
- " Ansible User : {{ hostvars[item].ansible_user | default('') }}"
20+
- " Alias : {{ hostvars[item].alias | default('') }}"
21+
- " VS Path : {{ hostvars[item].vspath | default('') }}"
22+
loop: "{{ ansible_play_hosts_all }}"
23+
run_once: true
24+
25+
# Include hosts aliases role to ansible
26+
- name: Include hosts aliases role to ansible
27+
ansible.builtin.include_role:
28+
name: ansible-role-hosts-aliases
29+
vars:
30+
user: "ansible"
31+
host: "ansible"
32+
home: "/home/ansible/"
33+
run_once: true

templates/hosts_aliases.j2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# {{ bash_hosts_aliases }}
22
# {{ comment }}
33

4-
{% for hostname, ip, alias, vspath, ssh_user in dev_hostnames | zip(dev_ips, dev_alias, dev_vspaths, dev_ansible_ssh_users) %}
5-
# {{ hostname }} ----- {{ ip }}
6-
alias ssh{{ alias }}='ssh {{ ssh_user }}@{{ hostname }}'
4+
{% for host in ansible_play_hosts_all %}
5+
# {{ host }} ----- {{ hostvars[host].ansible_host | default('') }}
6+
alias ssh{{ hostvars[host].alias | default(host) }}='ssh {{ hostvars[host].ansible_user | default("ansible") }}@{{ host }}'
77
{% if create_editor_aliases | default(true) | bool %}
8-
alias vs{{ alias }}='{{ editor_path }} --folder-uri "vscode-remote://ssh-remote+{{ ssh_user }}@{{ hostname }}{{ vspath }}"'
8+
alias vs{{ hostvars[host].alias | default(host) }}='{{ editor_path }} --folder-uri "vscode-remote://ssh-remote+{{ hostvars[host].ansible_user | default("ansible") }}@{{ host }}{{ hostvars[host].vspath | default("") }}"'
99

1010
{% else %}
1111

tests/test.yml

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
---
22
- name: Test hosts aliases role
33
hosts: localhost
4-
gather_facts: false
4+
gather_facts: true
55
vars:
6-
# Use actual hosts from inventory
7-
dev_hostnames: "{{ groups['all'] | difference(['localhost']) }}"
8-
dev_ips: []
9-
dev_alias: []
10-
dev_vspaths: []
11-
dev_ansible_ssh_users: []
12-
136
# Management host information (using /tmp for testing)
147
user: "{{ ansible_user_id }}"
158
host: "localhost"
@@ -28,26 +21,17 @@
2821
state: touch
2922
mode: '0644'
3023

31-
# Extract IPs from inventory
32-
- name: Set IPs from inventory
33-
ansible.builtin.set_fact:
34-
dev_ips: "{{ dev_ips + [hostvars[item].ansible_host | default('127.0.0.1')] }}"
35-
loop: "{{ dev_hostnames }}"
36-
37-
- name: Set aliases from inventory
38-
ansible.builtin.set_fact:
39-
dev_alias: "{{ dev_alias + [hostvars[item].alias | default(item)] }}"
40-
loop: "{{ dev_hostnames }}"
41-
42-
- name: Set vspaths from inventory
43-
ansible.builtin.set_fact:
44-
dev_vspaths: "{{ dev_vspaths + [hostvars[item].vspath | default('/home/' + hostvars[item].ansible_user + '/')] }}"
45-
loop: "{{ dev_hostnames }}"
46-
47-
- name: Set SSH users from inventory
48-
ansible.builtin.set_fact:
49-
dev_ansible_ssh_users: "{{ dev_ansible_ssh_users + [hostvars[item].ansible_user | default('ansible')] }}"
50-
loop: "{{ dev_hostnames }}"
24+
# Display discovered host information for testing
25+
- name: Display discovered host information
26+
ansible.builtin.debug:
27+
msg:
28+
- "Host: {{ item }}"
29+
- " IP : {{ hostvars[item].ansible_host | default('') }}"
30+
- " Ansible User : {{ hostvars[item].ansible_user | default('') }}"
31+
- " Alias : {{ hostvars[item].alias | default('') }}"
32+
- " VS Path : {{ hostvars[item].vspath | default('') }}"
33+
loop: "{{ groups['all'] | difference(['localhost']) }}"
34+
run_once: true
5135

5236
tasks:
5337
- name: Include test tasks

tests/test_tasks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
src: "../templates/hosts_aliases.j2" # noqa: no-relative-paths
3434
dest: "{{ home }}{{ bash_hosts_aliases }}"
3535
mode: '0644'
36+
owner: "{{ user }}"
3637
register: aliases_created

0 commit comments

Comments
 (0)