ansibleansible-module

Enhacing a playbook by storing the hostname of the changed machines locally


I am trying to enhance my playbook by storing the hostname of the changed machines locally, and I want to use as much as possible of the ansible module that is why I choosed the usage of th copy module for this storage:

My playbook look like this :

- name: test connectivity
  hosts: all
  tasks:
    - name: ping
      ping:
    - name: change default gateway address
      replace:
        path: /etc/network/interfaces
        regexp: '(up route add default gw [\d]*\.[\d]*.[\d]*)\.[\d]*$'
        replace: '\1.254'
        backup: yes
      when: (ansible_facts['distribution'] == "Debian")
    - name: restart networking service
      service:
        name: networking
        state: restarted
      when: (ansible_facts['distribution'] == "Debian")
    - name: change default gateway address on Redhat
      replace:
        path: /etc/sysconfig/network-scripts/ifcfg-eth0
        regexp: '(GATEWAY=[\d]*\.[\d]*.[\d]*)\.[\d]*$'
        replace: '\1.254'
        backup: yes
      when: (ansible_facts['distribution'] == "RedHat")
    - name: restart networking service for Redhat
      service:
        name: network
        state: restarted
      when: (ansible_facts['distribution'] == "RedHat")
    - name: register changed hosts locally
      copy:
        content: "{{ ansible_facts['hostname'] }}"
        dest: "/tmp/changed.txt"
        delegate_to: localhost

but the following error keep on popping :

    TASK [register changed hosts locally] *******************************************************************************************************************************************************************************************************
fatal: [name of host 1]: FAILED! => {"changed": false, "checksum": "df1496ad2c4ffed5abfad0a9fc69f7fb3a039765", "msg": "Unsupported parameters for (copy) module: delegate_to Supported parameters include: _original_basename, attributes, backup, checksum, content, delimiter, dest, directory_mode, follow, force, group, local_follow, mode, owner, regexp, remote_src, selevel, serole, setype, seuser, src, unsafe_writes, validate"}
fatal: [name of host 2]: FAILED! => {"changed": false, "checksum": "b75217f7ab69a82dd5aea389fcd8eacee15743e5", "msg": "Unsupported parameters for (copy) module: delegate_to Supported parameters include: _original_basename, attributes, backup, checksum, content, delimiter, dest, directory_mode, follow, force, group, local_follow, mode, owner, regexp, remote_src, selevel, serole, setype, seuser, src, unsafe_writes, validate"}

so I want to know is it due to the copy parameters I use if yes? which one exactly ?


Solution

  • - name: register changed hosts locally
      copy:
        content: "{{ ansible_facts['hostname'] }}"
        dest: "/tmp/changed.txt"
      delegate_to: localhost
    

    delegate_to option needs to be under copy.

    Alternatively see remote_src option https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html#parameter-remote_src