main.yml 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. - name: system
  2. blockinfile:
  3. dest: /etc/ssh/ssh_config
  4. marker: '# ANSIBLE SSH CONFIG {mark}'
  5. block: |-
  6. {% for host in ssh_config_hosts %}
  7. {% set host_config = ssh_config_hosts[host] %}
  8. Host {{host}}
  9. {% if host_config.hostname is defined %}
  10. HostName {{host_config.hostname}}
  11. {% endif %}
  12. {% if host_config.port is defined %}
  13. Port {{host_config.port}}
  14. {% endif %}
  15. {% if host_config.user is defined %}
  16. User {{host_config.user}}
  17. {% endif %}
  18. {% if host_config.key_path is defined %}
  19. IdentityFile "{{host_config.key_path}}"
  20. {% endif %}
  21. {% if host_config.proxy_command is defined %}
  22. ProxyCommand {{host_config.proxy_command}}
  23. {% elif host_config.ssh_proxy is defined %}
  24. ProxyCommand ssh -W localhost:{{host_config.ssh_proxy.port}}
  25. {%- if host_config.ssh_proxy.user is defined %}
  26. -o User={{host_config.ssh_proxy.user}}
  27. {%- endif %}
  28. {%- if host_config.ssh_proxy.key_path is defined %}
  29. -o IdentityFile='{{host_config.ssh_proxy.key_path}}'
  30. {%- endif %}
  31. {{host_config.ssh_proxy.host}}
  32. {% endif %}
  33. {% endfor %}
  34. become: yes
  35. - name: users
  36. blockinfile:
  37. dest: '~/.ssh/config'
  38. create: yes
  39. mode: 0600
  40. marker: '# ANSIBLE SSH CONFIG {mark}'
  41. block: |-
  42. {% set hosts = ssh_config_user_hosts[item] %}
  43. {% for host in hosts %}
  44. {% set host_config = hosts[host] %}
  45. Host {{host}}
  46. {% if host_config.hostname is defined %}
  47. HostName {{host_config.hostname}}
  48. {% endif %}
  49. {% if host_config.port is defined %}
  50. Port {{host_config.port}}
  51. {% endif %}
  52. {% if host_config.user is defined %}
  53. User {{host_config.user}}
  54. {% endif %}
  55. {% if host_config.key_path is defined %}
  56. IdentityFile "{{host_config.key_path}}"
  57. {% endif %}
  58. {% if host_config.ssh_proxy is defined %}
  59. ProxyCommand ssh -W localhost:{{host_config.ssh_proxy.port}}
  60. {%- if host_config.ssh_proxy.user is defined %}
  61. -o User={{host_config.ssh_proxy.user}}
  62. {%- endif %}
  63. {%- if host_config.ssh_proxy.key_path is defined %}
  64. -o IdentityFile='{{host_config.ssh_proxy.key_path}}'
  65. {%- endif %}
  66. {{host_config.ssh_proxy.host}}
  67. {% endif %}
  68. {% endfor %}
  69. become: yes
  70. become_user: '{{item}}'
  71. with_items: '{{ssh_config_user_hosts}}'