From c824cee30b41b534f665bccbb1c4dae285b472c6 Mon Sep 17 00:00:00 2001 From: AndreyKiryliuk Date: Thu, 15 Mar 2018 22:39:52 +0300 Subject: [PATCH 1/5] Add ssh_config_host_prefix ssh_config_default_user Add ssh_config_host_prefix and ssh_config_default_user --- defaults/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/defaults/main.yml b/defaults/main.yml index 66d37c0..52e87ec 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -3,3 +3,5 @@ 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: From 20c2adc3e869efb61b3560a8ffbc4bf62490a4a4 Mon Sep 17 00:00:00 2001 From: AndreyKiryliuk Date: Thu, 15 Mar 2018 22:56:24 +0300 Subject: [PATCH 2/5] add vars for prefix and user add ssh_config_force_user ssh_config_default_user ssh_config_host_prefix --- templates/ssh_config.j2 | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/templates/ssh_config.j2 b/templates/ssh_config.j2 index 64a4054..f30e3bc 100644 --- a/templates/ssh_config.j2 +++ b/templates/ssh_config.j2 @@ -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'] }} @@ -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'] }} From c2f9dcf403b83575e3ecbf2016e23c24aceee54c Mon Sep 17 00:00:00 2001 From: AndreyKiryliuk Date: Thu, 15 Mar 2018 23:06:55 +0300 Subject: [PATCH 3/5] add ssh_config_project_name add ssh_config_project_name for multiple project use --- tasks/main.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tasks/main.yml b/tasks/main.yml index cb59540..ce82627 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -5,12 +5,14 @@ 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}}') }}" @@ -18,6 +20,6 @@ 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 }}" From fb12b8206a94410ab55ef2f664480b9b0ca2d436 Mon Sep 17 00:00:00 2001 From: AndreyKiryliuk Date: Thu, 15 Mar 2018 23:07:42 +0300 Subject: [PATCH 4/5] add ssh_config_project_name add ssh_config_project_name for multiple project use --- defaults/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/defaults/main.yml b/defaults/main.yml index 52e87ec..f2d14f9 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -5,3 +5,4 @@ ssh_config_file: "{{ ssh_configs_dir }}/ssh_config" inventory_groups: ["all"] ssh_config_host_prefix: ssh_config_default_user: +ssh_config_project_name: From 17d9123e06a708e0fad6641ebaba3c7e54daaabf Mon Sep 17 00:00:00 2001 From: AndreyKiryliuk Date: Thu, 15 Mar 2018 23:30:55 +0300 Subject: [PATCH 5/5] add vars for prefix and user and project name add ssh_config_force_user ssh_config_default_user ssh_config_host_prefix ssh_config_project_name --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 5dcb387..f19f502 100644 --- a/README.md +++ b/README.md @@ -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. | +| `ssh_config_host_prefix` | `` | Prefix for add to hostname. | +| `ssh_config_default_user` | `` | Default user. For use when ansible_host not set. | +| `ssh_config_force_user` | `` | Force rewrite user. | + ### Role Consumed Variables