通过在本地存储更改机器的主机名来增强剧本

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

我试图通过在本地存储更改后的机器的主机名来增强我的剧本,我想尽可能多地使用 ansible 模块,这就是为什么我选择使用 th copy 模块来存储:

我的剧本是这样的:

- 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: '.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: '.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

但不断出现以下错误:

    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"}

所以我想知道是否是因为我使用的复制参数,如果是的话?到底是哪一个?

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

delegate_to选项需要在copy下

或者查看 remote_src 选项 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html#parameter-remote_src

以下是我为让它发挥作用所做的工作:


 - name: log the changed hosts
      local_action:
        module : lineinfile
        line: "{{ ansible_facts['hostname'] }} {{item}}"
        dest: /tmp/changed.txt
      with_items:
        - "{{debresult}}"
        - "{{redresult}}"