Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add vars for prefix and user and project name #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Role Variables
|`ssh_config_file`|`{{ssh_configs_dir}}/ssh_config`|Where should the SSH client configuration be written to? Most implementations use `~/.ssh/config` so you can change this if you want.|
|`inventory_groups`|`["all"]`|Which inventory groups should we read to create SSH client configuration for? By default the built-in group `all` will be used since it should always be valid. `ungrouped` is also a built-in group name. To get a full list of groups in your Ansible directory, use `ansible -m debug -a 'var=groups.keys()\|sort' localhost`.|
|`keepgroupnames`|`"False"`|Should ansible groupname(s) be used to create a pattern for hostname ? When a server is part of several groups, then additionnal patterns will be created to match each and every group. When set to `"True"` and with inventory file example below, `ssh server1` **and** `ssh production.server1` will both work.|
| `ssh_config_project_name` | `` | Project name for use in marker. |
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to stick with using the group as the marker as "project name" seems too implementation specific. Maybe there just needs to be a generic variable for modifying the marker so someone could add anything they want.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example?

| `ssh_config_host_prefix` | `` | Prefix for add to hostname. |
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not completely understanding the use case for a host prefix. Can you elaborate more? It sounds like something specific to your environment maybe?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On some servers I use two different logins.
one with root rights.
and the second for working with the deployed application
and I connect to them in different ways, for example:
ssh rapp # for rott access
ssh app # for app user access

| `ssh_config_default_user` | `` | Default user. For use when ansible_host not set. |
| `ssh_config_force_user` | `` | Force rewrite user. |



### Role Consumed Variables
Expand Down
3 changes: 3 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
ssh_configs_dir: "{{ playbook_dir }}/ssh_configs"
ssh_config_file: "{{ ssh_configs_dir }}/ssh_config"
inventory_groups: ["all"]
ssh_config_host_prefix:
ssh_config_default_user:
ssh_config_project_name:
4 changes: 3 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
path: "{{ ssh_configs_dir }}"
state: directory
mode: 0755

- name: create a local config file
template:
src: "ssh_config.j2"
dest: "{{ ssh_configs_dir }}/ssh_config_{{item}}"
with_items:
- "{{ inventory_groups }}"

- name: Merge the config with your local SSH config
blockinfile:
block: "{{ lookup('file', '{{ ssh_configs_dir }}/ssh_config_{{item}}') }}"
dest: "{{ ssh_config_file }}"
backup: yes
create: yes
insertafter: EOF
marker: "# {mark} ANSIBLE MANAGED BLOCK (ssh_config_from_inventory) {{item}}"
marker: "# {mark} ANSIBLE MANAGED BLOCK (ssh_config_from_inventory) {{ ssh_config_project_name }} {{item}}"
with_items:
- "{{ inventory_groups }}"
10 changes: 7 additions & 3 deletions templates/ssh_config.j2
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{% for host in groups[item] %}
{% if keepgroupnames is defined and keepgroupnames == "True" %}
Host {{ hostvars[host]['inventory_hostname_short'] }}{% for group in hostvars[host]['group_names'] %} {{ group }}.{{ hostvars[host]['inventory_hostname_short'] }}{% endfor %}
Host {{ ssh_config_host_prefix }}{{ hostvars[host]['inventory_hostname_short'] }}{% for group in hostvars[host]['group_names'] %} {{ group }}.{{ ssh_config_host_prefix }}{{ hostvars[host]['inventory_hostname_short'] }}{% endfor %}

{% else %}
Host {{ hostvars[host]['inventory_hostname_short'] }}
Host {{ ssh_config_host_prefix }}{{ hostvars[host]['inventory_hostname_short'] }}
{% endif %}
{% if hostvars[host]['ansible_host'] is defined %}
HostName {{ hostvars[host]['ansible_host'] }}
Expand All @@ -17,10 +17,14 @@ Host {{ hostvars[host]['inventory_hostname_short'] }}
{% elif hostvars[host]['ansible_ssh_port'] is defined %}
Port {{ hostvars[host]['ansible_ssh_port'] }}
{% endif %}
{% if hostvars[host]['ansible_user'] is defined %}
{% if ssh_config_force_user is defined and ssh_config_force_user != '' %}
User {{ ssh_config_force_user }}
{% elif hostvars[host]['ansible_user'] is defined %}
User {{ hostvars[host]['ansible_user'] }}
{% elif hostvars[host]['ansible_ssh_user'] is defined %}
User {{ hostvars[host]['ansible_ssh_user'] }}
{% elif ssh_config_default_user is defined and ssh_config_default_user != '' %}
User {{ ssh_config_default_user }}
{% endif %}
{% if hostvars[host]['ansible_ssh_private_key_file'] is defined %}
IdentityFile {{ hostvars[host]['ansible_ssh_private_key_file'] }}
Expand Down