- name: system blockinfile: dest: /etc/ssh/ssh_config marker: '# ANSIBLE SSH CONFIG {mark}' block: |- {% for host in ssh_config_hosts %} {% set host_config = ssh_config_hosts[host] %} Host {{host}} {% if host_config.hostname is defined %} HostName {{host_config.hostname}} {% endif %} {% if host_config.user is defined %} User {{host_config.user}} {% endif %} {% if host_config.ssh_proxy is defined %} ProxyCommand ssh -W localhost:{{host_config.ssh_proxy.port}} {{host_config.ssh_proxy.host}} {% endif %} {% endfor %} become: yes - name: users blockinfile: dest: '~/.ssh/config' create: yes mode: 0600 marker: '# ANSIBLE SSH CONFIG {mark}' block: |- {% set hosts = ssh_config_user_hosts[item] %} {% for host in hosts %} {% set host_config = hosts[host] %} Host {{host}} {% if host_config.hostname is defined %} HostName {{host_config.hostname}} {% endif %} {% if host_config.user is defined %} User {{host_config.user}} {% endif %} {% if host_config.ssh_proxy is defined %} ProxyCommand ssh -W localhost:{{host_config.ssh_proxy.port}} {{host_config.ssh_proxy.host}} {% endif %} {% endfor %} become: yes become_user: '{{item}}' with_items: '{{ssh_config_user_hosts}}'