Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何保留命令输出中的换行符,并使用ansible copy module将其写入文件_Linux_Ansible - Fatal编程技术网

Linux 如何保留命令输出中的换行符,并使用ansible copy module将其写入文件

Linux 如何保留命令输出中的换行符,并使用ansible copy module将其写入文件,linux,ansible,Linux,Ansible,我试图在不丢失换行符的情况下将linux find命令的输出复制到一个平面文件中,但我似乎找不到这样做的方法。文件内容始终没有换行符。任何想法都将不胜感激 我的代码: - name: Play to run find command and capture its output to a file hosts: all tasks: - name: 'Run find command to fetch file rights {{inventory_hostname}}'

我试图在不丢失换行符的情况下将linux find命令的输出复制到一个平面文件中,但我似乎找不到这样做的方法。文件内容始终没有换行符。任何想法都将不胜感激

我的代码:

- name: Play to run find command and capture its output to a file
  hosts: all

  tasks:

    - name: 'Run find command to fetch file rights {{inventory_hostname}}'
      command: 'find /var/log/rhsm -type f -printf "{{ inventory_hostname }},%m,%p;\n"' 
      register: find_results
      become: true
      become_user: root
      become_method: sudo

    - name: Print to verify it works
      debug: 
        msg: '{{find_results.stdout}}'

    - name: Use copy module to create the file using output from the previous command.
      copy: 
        dest: "/tmp/find_results.txt"
        content: >
            "{{ find_results.stdout_lines  | list }}"
      delegate_to: localhost

    ### This doesn't work either
    # - name: Use blockinfile to do the same
    #   blockinfile:
    #     path: "/tmp/find_results.txt"
    #     block: |
    #         "{{ find_results.stdout_lines }}"
我的输出:

$ cat /tmp/find_results.txt
"[u'svrt1,644,/var/log/rhsm/rhsm.log-20210221;', u'svrt1,644,/var/log/rhsm/rhsm.log-20210228;', u'svrt1,644,/var/log/rhsm/rhsm.log-20210307;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log-20210325;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log;', u'svrt1,644,/var/log/rhsm/rhsm.log;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log-20210221;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log-20210228;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log-20210307;', u'svrt1,644,/var/log/rhsm/rhsm.log-20210325;']"
预期产出:

$ cat /tmp/find_results.txt
    svrt1,644,/var/log/rhsm/rhsm.log-20210221;
    svrt1,644,/var/log/rhsm/rhsm.log-20210228;
    svrt1,644,/var/log/rhsm/rhsm.log-20210307;
    svrt1,644,/var/log/rhsm/rhsmcertd.log-20210325;
    svrt1,644,/var/log/rhsm/rhsmcertd.log;
    svrt1,644,/var/log/rhsm/rhsm.log;
    svrt1,644,/var/log/rhsm/rhsmcertd.log-20210221;
    svrt1,644,/var/log/rhsm/rhsmcertd.log-20210228;
    svrt1,644,/var/log/rhsm/rhsmcertd.log-20210307;
    svrt1,644,/var/log/rhsm/rhsm.log-20210325;

堵塞后不要使用管道。仅使用“查找结果”。不是这样的标准线

[rohtash@172 blockinfile]$ ansible-playbook find_withblock.yml 

PLAY [Play to run find command and capture its output to a file] ******************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [localhost]

TASK [Run find command to fetch file rights localhost] ****************************************************************************************************************************************
changed: [localhost]

TASK [Print to verify it works] ***************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "localhost,664,/var/tmp/rhsm/abc1;\nlocalhost,664,/var/tmp/rhsm/abc2;\nlocalhost,664,/var/tmp/rhsm/abc3;\nlocalhost,664,/var/tmp/rhsm/abc4;\nlocalhost,664,/var/tmp/rhsm/abc5;\nlocalhost,664,/var/tmp/rhsm/abc6;"
}

TASK [Use blockinfile to do the same] *********************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[rohtash@172 blockinfile]$ cat /tmp/find_results_usingblocks.txt
# BEGIN ANSIBLE MANAGED BLOCK
localhost,664,/var/tmp/rhsm/abc1;
localhost,664,/var/tmp/rhsm/abc2;
localhost,664,/var/tmp/rhsm/abc3;
localhost,664,/var/tmp/rhsm/abc4;
localhost,664,/var/tmp/rhsm/abc5;
localhost,664,/var/tmp/rhsm/abc6;
# END ANSIBLE MANAGED BLOCK
[rohtash@172 blockinfile]$ cat find_withblock.yml 
- name: Play to run find command and capture its output to a file
  hosts: localhost
  connection: local
  tasks:
    - name: 'Run find command to fetch file rights {{inventory_hostname}}'
      command: 'find /var/tmp/rhsm -type f -printf "{{ inventory_hostname }},%m,%p;\n"' 
      register: find_results
      become: true
      become_user: root
      become_method: sudo

- name: Print to verify it works
  debug: 
    msg: '{{find_results.stdout}}'

- name: Use blockinfile to do the same
  blockinfile:
      path: "/tmp/find_results_usingblocks.txt"
      block: "{{ find_results.stdout }}"
      state: present
  delegate_to: localhost
选择最适合你的

堵塞后不要使用管道。仅使用“查找结果”。不是这样的标准线

[rohtash@172 blockinfile]$ ansible-playbook find_withblock.yml 

PLAY [Play to run find command and capture its output to a file] ******************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [localhost]

TASK [Run find command to fetch file rights localhost] ****************************************************************************************************************************************
changed: [localhost]

TASK [Print to verify it works] ***************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "localhost,664,/var/tmp/rhsm/abc1;\nlocalhost,664,/var/tmp/rhsm/abc2;\nlocalhost,664,/var/tmp/rhsm/abc3;\nlocalhost,664,/var/tmp/rhsm/abc4;\nlocalhost,664,/var/tmp/rhsm/abc5;\nlocalhost,664,/var/tmp/rhsm/abc6;"
}

TASK [Use blockinfile to do the same] *********************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[rohtash@172 blockinfile]$ cat /tmp/find_results_usingblocks.txt
# BEGIN ANSIBLE MANAGED BLOCK
localhost,664,/var/tmp/rhsm/abc1;
localhost,664,/var/tmp/rhsm/abc2;
localhost,664,/var/tmp/rhsm/abc3;
localhost,664,/var/tmp/rhsm/abc4;
localhost,664,/var/tmp/rhsm/abc5;
localhost,664,/var/tmp/rhsm/abc6;
# END ANSIBLE MANAGED BLOCK
[rohtash@172 blockinfile]$ cat find_withblock.yml 
- name: Play to run find command and capture its output to a file
  hosts: localhost
  connection: local
  tasks:
    - name: 'Run find command to fetch file rights {{inventory_hostname}}'
      command: 'find /var/tmp/rhsm -type f -printf "{{ inventory_hostname }},%m,%p;\n"' 
      register: find_results
      become: true
      become_user: root
      become_method: sudo

- name: Print to verify it works
  debug: 
    msg: '{{find_results.stdout}}'

- name: Use blockinfile to do the same
  blockinfile:
      path: "/tmp/find_results_usingblocks.txt"
      block: "{{ find_results.stdout }}"
      state: present
  delegate_to: localhost

选择最适合你的。

谢谢你抽出时间。。。不幸的是,这没有任何区别。我已经尝试了所有这些|、|-、>等以及其他一些特技,比如转换为列表,尝试在行分隔符上替换regex_等。这是一件简单的事情,但我不敢相信这是我上面给出的一个PITAcopy精确代码,并向我发送了一个可行的playbook命令结果。如果它在我的ansible设置上工作,它也应该在你的上工作。是的,我已经这样做了。不起作用。对你有用吗?还通过在多个文本编辑器中打开来检查输出。不走运,是的,我明白了[rohtash@172blockinfle]$cat/tmp/find_results.txt复制我编写的精确代码并显示您的cat/tmp/find_结果。txt感谢您花时间。。。不幸的是,这没有任何区别。我已经尝试了所有这些|、|-、>等以及其他一些特技,比如转换为列表,尝试在行分隔符上替换regex_等。这是一件简单的事情,但我不敢相信这是我上面给出的一个PITAcopy精确代码,并向我发送了一个可行的playbook命令结果。如果它在我的ansible设置上工作,它也应该在你的上工作。是的,我已经这样做了。不起作用。对你有用吗?还通过在多个文本编辑器中打开来检查输出。不走运,是的,我明白了[rohtash@172blockinfile]$cat/tmp/find_results.txt复制我编写的精确代码并显示您的cat/tmp/find_results.txt