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

时间:2021-02-05 15:48:58

标签: ansible ansible-module

我正在尝试通过在本地存储已更改机器的主机名来增强我的剧本,并且我想尽可能多地使用 ansible 模块,这就是我选择使用 th copy 模块进行此存储的原因:

我的剧本是这样的:

/node_modules/intl-tel-input/build/...

但以下错误不断弹出:

- 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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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


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