Here’s the play I came up with, and I listed the inventory in the order that I want within the group. I would like my config file to be in the following order:
Host node1 10.0.0.30
HostName 10.0.0.30
...
Host node2 10.0.0.31
HostName 10.0.0.30
...
Host node3 10.0.0.32
HostName 10.0.0.30
...
My current attempt at achieving this
I swear it worked once and then just stopped after I tweaked it a little more…
- name: Generate SSH config sequentially by LAN IP
delegate_to: localhost
community.general.ssh_config:
ssh_config_file: "{{ lookup('env', 'HOME') }}/.ssh/config"
state: present
host: "{{ item.inventory_hostname }}"
hostname: "{{ item.ansible_host }}"
remote_user: "{{ item.ansible_user }}"
port: "{{ item.hostport | default(22) }}"
identity_file: "{{ item.ssh_privatekey }}"
loop: "{{ play_hosts | map('extract', hostvars) | sort(attribute='ansible_host') }}"