--- - block: - name: add ip address of all hosts to all hosts lineinfile: dest: /etc/hosts regexp: '.*{{ item }} {{ item.split(".")[0] }}$' line: "{{ hostvars[item].ansible_host }} {{ item }} {{ item.split('.')[0] }}" state: present when: - hostvars[item].ansible_host is defined - pin_hosts with_items: "{{ groups.all }}" tags: - hosts become: true