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 diff --git a/defaults/main.yml b/defaults/main.yml index 66d37c0..f2d14f9 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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: 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 }}" 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'] }}