main.yml 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. {%- if host_config.ssh_proxy.bastion_port is defined %}
  32. -p {{ host_config.ssh_proxy.bastion_port }}
  33. {%- endif %}
  34. {{host_config.ssh_proxy.host}}
  35. {% endif %}
  36. {% endfor %}
  37. become: yes
  38. - name: users
  39. blockinfile:
  40. dest: '~/.ssh/config'
  41. create: yes
  42. mode: 0600
  43. marker: '# ANSIBLE SSH CONFIG {mark}'
  44. block: |-
  45. {% set hosts = ssh_config_user_hosts[item] %}
  46. {% for host in hosts %}
  47. {% set host_config = hosts[host] %}
  48. Host {{host}}
  49. {% if host_config.hostname is defined %}
  50. HostName {{host_config.hostname}}
  51. {% endif %}
  52. {% if host_config.port is defined %}
  53. Port {{host_config.port}}
  54. {% endif %}
  55. {% if host_config.user is defined %}
  56. User {{host_config.user}}
  57. {% endif %}
  58. {% if host_config.key_path is defined %}
  59. IdentityFile "{{host_config.key_path}}"
  60. {% endif %}
  61. {% if host_config.ssh_proxy is defined %}
  62. ProxyCommand ssh -W localhost:{{host_config.ssh_proxy.port}}
  63. {%- if host_config.ssh_proxy.user is defined %}
  64. -o User={{host_config.ssh_proxy.user}}
  65. {%- endif %}
  66. {%- if host_config.ssh_proxy.key_path is defined %}
  67. -o IdentityFile='{{host_config.ssh_proxy.key_path}}'
  68. {%- endif %}
  69. {{host_config.ssh_proxy.host}}
  70. {% endif %}
  71. {% endfor %}
  72. become: yes
  73. become_user: '{{item}}'
  74. with_items: '{{ssh_config_user_hosts}}'