0

我试图通过在本地存储更改的机器的主机名来增强我的剧本,并且我想尽可能多地使用 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: '\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

但以下错误不断弹出:

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

所以我想知道是不是因为我使用的复制参数?究竟是哪一个?

4

2 回答 2

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

delegate_to 选项需要在副本下。

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

于 2021-02-05T16:39:53.690 回答
0

这是我为使其工作所做的工作:


 - name: log the changed hosts
      local_action:
        module : lineinfile
        line: "{{ ansible_facts['hostname'] }} {{item}}"
        dest: /tmp/changed.txt
      with_items:
        - "{{debresult}}"
        - "{{redresult}}"
于 2021-02-07T17:07:54.383 回答