繁体   English   中英

需要将输出附加到文件

[英]Need to append output to a file

在下面的代码中,将重复运行多达8次,每次输出都会有所不同。 我需要将该输出保存到文件中,而不将先前添加的输出删除到同一文件中。

但是在这里,它将删除先前的输出,并将新的输出添加到该文件。

因此输出将仅是最后一个。

您能帮我找到解决方案吗?

  - name: save output to config backup directory
    copy:
      content: " {{ output.stdout[0] }}{{output.stdout[1] }}"
      dest: /home/hhh.ghydggxxxx.xx/paxxxx/playbook-xx/vlan351/port_output/{{inventory_hostname}}.txt
    delegate_to: localhost

您可以在文件名的末尾添加ansible变量:{{ansible_date_time.epoch}},例如:

- name: save output to config backup directory 
  copy: 
    content: "{{ output.stdout[0] }}{{ output.stdout[1] }}" 
    dest: /home/hhh.ghydggxxxx.xx/paxxxx/playbook-xx/vlan351/port_output/{{inventory_hostname}}-{{ ansible_date_time.epoch }}.txt 
  delegate_to: localhost

或使用另一个“ date_time”变量:

"ansible_date_time": {"date": "2019-09-05", "day": "05", "epoch": "1567681843", "hour": "13", "iso8601": "2019-09-05T11:10:43Z", "iso8601_basic": "20190905T131043506678", "iso8601_basic_short": "20190905T131043", "iso8601_micro": "2019-09-05T11:10:43.506928Z", "minute": "10", "month": "09", "second": "43", "time": "13:10:43", "tz": "CEST", "tz_offset": "+0200", "weekday": "jueves", "weekday_number": "4", "weeknumber": "35", "year": "2019"}

尝试这样的事情,

- name: Add contents to a file and appends the data in file
  lineinfile:
    path: /root/testfile
    line: "{{ output.stdout }}"
    create: yes

此任务将行(即output.stdout到文件,并且始终将数据追加到文件,而不会覆盖内容。

如果文件不存在,则使用create special属性创建文件。

根据您的用例进行配置。 我创建了以下示例,

- name: save output to config backup directory
  lineinfile:
    line: " {{ output.stdout[0] }}{{output.stdout[1] }}"
    path: "/home/hhh.ghydggxxxx.xx/paxxxx/playbook-xx/vlan351/port_output/{{inventory_hostname}}.txt"
    create: yes
  delegate_to: localhost

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM