main.yml 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.user is defined %}
  13. User {{host_config.user}}
  14. {% endif %}
  15. {% if host_config.key_path is defined %}
  16. IdentityFile '{{host_config.key_path}}'
  17. {% endif %}
  18. {% if host_config.ssh_proxy is defined %}
  19. ProxyCommand ssh -W localhost:{{host_config.ssh_proxy.port}}
  20. {%- if host_config.ssh_proxy.user is defined %}
  21. -o User={{host_config.ssh_proxy.user}}
  22. {%- endif %}
  23. {%- if host_config.ssh_proxy.key_path is defined %}
  24. -i '{{host_config.ssh_proxy.key_path}}'
  25. {%- endif %}
  26. {{host_config.ssh_proxy.host}}
  27. {% endif %}
  28. {% endfor %}
  29. become: yes
  30. - name: users
  31. blockinfile:
  32. dest: '~/.ssh/config'
  33. create: yes
  34. mode: 0600
  35. marker: '# ANSIBLE SSH CONFIG {mark}'
  36. block: |-
  37. {% set hosts = ssh_config_user_hosts[item] %}
  38. {% for host in hosts %}
  39. {% set host_config = hosts[host] %}
  40. Host {{host}}
  41. {% if host_config.hostname is defined %}
  42. HostName {{host_config.hostname}}
  43. {% endif %}
  44. {% if host_config.user is defined %}
  45. User {{host_config.user}}
  46. {% endif %}
  47. {% if host_config.key_path is defined %}
  48. IdentityFile '{{host_config.key_path}}'
  49. {% endif %}
  50. {% if host_config.ssh_proxy is defined %}
  51. ProxyCommand ssh -W localhost:{{host_config.ssh_proxy.port}}
  52. {%- if host_config.ssh_proxy.user is defined %}
  53. -o User={{host_config.ssh_proxy.user}}
  54. {%- endif %}
  55. {%- if host_config.ssh_proxy.key_path is defined %}
  56. -i '{{host_config.ssh_proxy.key_path}}'
  57. {%- endif %}
  58. {{host_config.ssh_proxy.host}}
  59. {% endif %}
  60. {% endfor %}
  61. become: yes
  62. become_user: '{{item}}'
  63. with_items: '{{ssh_config_user_hosts}}'