Conditional copy based on the existence of a directory

Dear Colleagues,

I’d like to be able to copy/template a file to a directory on a remote host depending upon the existence of the directory there (from a list of possible variants). Below I publish what I currently use but I hope there is a more scalable and elegant method, can you advise one?

  • name: check for new zabbix
    stat:
    path: /etc/zabbix/zabbix_agentd.d/
    register: newpath

  • name: check for old zabbix
    stat:
    path: /etc/zabbix/zabbix_agentd.conf.d/
    register: oldpath

  • name: copy consul userparameters for new zabbix
    when: newpath.stat.exists and newpath.stat.isdir
    template:
    src: files/consul-userparameter.conf.j2
    dest: /etc/zabbix/zabbix_agentd.d/consul.conf

  • name: copy consul userparameters for old zabbix
    when: oldpath.stat.exists and oldpath.stat.isdir
    template:
    src: files/consul-userparameter.conf.j2
    dest: /etc/zabbix/zabbix_agentd.conf.d/consul.conf