main.yml 2.3 KB

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