Ansible shell模块问题

Ansible shell模块问题

我需要使用ansible中的shell模块清除远程主机上的文件内容,但无法这样做

---
 - hosts: ansi1
   become: yes
   gather_facts: no
   tasks:
   - name: checking shell power
     shell:
        >/tmp/1.txt
     args:
      executable: /bin/bash

错误:

ERROR! Syntax Error while loading YAML.


The error appears to have been in '/etc/ansible/shell.yml': line 8, column 10, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

     shell:
        >/tmp/1.txt
         ^ here

答案1

@Jeff Schaller 这有助于解决问题,,

---
 - hosts: ansi2
   become: yes
   gather_facts: no
   tasks:
   - name: checking shell power
     shell:
             '>/tmp/1.txt'
     args:
      executable: /bin/bash

现在如果我有多个命令要运行怎么办

答案2

您可以将其用于多个命令,如下所示:

    - name: Copy var directory
      shell: |
        cmd1
        cmd2
        cmd3
      args:
        executable: /bin/bash

相关内容