Linux 需要将输出附加到文件
在下面的代码中将重复运行多达8次,每次输出都会不同。我需要将该输出保存在一个文件中,而不删除以前添加到同一文件中的输出 但在这里,它会删除以前的输出,并将新输出添加到该文件中 因此,输出将仅为最后一个 你能帮我找到解决办法吗Linux 需要将输出附加到文件,linux,shell,networking,ansible,Linux,Shell,Networking,Ansible,在下面的代码中将重复运行多达8次,每次输出都会不同。我需要将该输出保存在一个文件中,而不删除以前添加到同一文件中的输出 但在这里,它会删除以前的输出,并将新输出添加到该文件中 因此,输出将仅为最后一个 你能帮我找到解决办法吗 - name: save output to config backup directory copy: content: " {{ output.stdout[0] }}{{output.stdout[1] }}" dest: /hom
- 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\u date\u 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
或者使用另一个“日期时间”变量:
试试这个
- name: Add contents to a file and appends the data in file
lineinfile:
path: /root/testfile
line: "{{ output.stdout }}"
create: yes
此任务将行(即,output.stdout
)添加到文件中,并始终在不覆盖内容的情况下将数据附加到文件中
如果文件不存在,create
特殊属性用于创建文件
根据您的用例配置它。我已经创建了以下示例
- 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
不清楚,请在您的帖子中添加更多详细信息。另外,请将您的样本包装在代码标签中,然后告知我们。可能的副本等。
- 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