automated creation of a configuration file with ansible (or something else)

Q: "Read a data file, search a string (example:hostname contains "NR") ... choose the good template in a library in order to create the configuration file."

For example, given the data files

> ssh admin@test_11 cat /tmp/hostname
hostname-NR

> ssh admin@test_12 cat /tmp/hostname
hostname-NS

> ssh admin@test_13 cat /tmp/hostname
hostname-NX

and the templates

> cat templates/nr.j2 
# template nr.j2

> cat templates/ns.j2 
# template ns.j2

> cat templates/nt.j2 
# template nt.j2

> cat templates/default.j2 
# template default.j2

the play below

- hosts: test_11,test_12,test_13
  vars:
    templates_lib:
      - {contains: "{{ my_hostname is search('NR') }}", template: nr.j2}
      - {contains: "{{ my_hostname is search('NS') }}", template: ns.j2}
      - {contains: "{{ my_hostname is search('NT') }}", template: nt.j2}
  tasks:
    - command: cat /tmp/hostname
      register: result
    - template:
        src: "{{ my_template) }}"
        dest: /tmp/test.conf
      vars:
        my_hostname: "{{ result.stdout }}"
        my_template: "{{ templates_lib|
                         selectattr('contains')|
                         map(attribute='template')|
                         first|default('default.j2') }}"

gives

> ssh admin@test_11 cat /tmp/test.conf
# template nr.j2

> ssh admin@test_12 cat /tmp/test.conf
# template ns.j2

> ssh admin@test_13 cat /tmp/test.conf
# template default.j2