Answers: <blockquote>
  <p><em>I do not see why the first should fail.</em></p>
  </blockquote>
  <p>It looks like the task has a syntax error</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: &quot;Delete existing keys and de-arm&quot;
    become: true
    shell: |
       &quot;rm -rf /usr/share/keyrings/jenkins.gpg&quot;
       &quot;gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key&quot;
  </code></pre>
  <p>According the error message</p>
  <pre class="lang-yaml prettyprint-override"><code>    &quot;stderr&quot;: &quot;/bin/sh: 1: rm -rf /usr/share/keyrings/jenkins.gpg: not found\n/bin/sh: 2: gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key: not found&quot;,
      &quot;stderr_lines&quot;: [
          &quot;/bin/sh: 1: rm -rf /usr/share/keyrings/jenkins.gpg: not found&quot;,
          &quot;/bin/sh: 2: gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key: not found&quot;
  </code></pre>
  <p>The lines are processed as one single command and if I interpret it correctly. Furthermore, no quotes are necessary.</p>
  <blockquote>
  <p><em>I don't know what <code>not found</code> means in this context.</em></p>
  </blockquote>
  <p>It means that the command as a whole or parts of it wasn't found, a syntax error and is the expected behavior.</p>
  <p><strong>Possible Solution</strong></p>
  <p>You could try with</p>
  <pre class="lang-yaml prettyprint-override"><code>  - name: Exec sh script on Remote Node
      shell:
        cmd: |
          rm -f /usr/share/keyrings/jenkins.gpg
          gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key
      register: result
  </code></pre>
  <p>or even with</p>
  <pre class="lang-yaml prettyprint-override"><code>    shell: &quot;rm -f /usr/share/keyrings/jenkins.gpg &amp;&amp; gpg --dearmor -o /usr/share/keyrings/jenkins.gpg&quot;
  </code></pre>
  <p><strong>Similar Q&amp;A</strong></p>
  <ul>
  <li><a href="https://stackoverflow.com/a/73594837/6771046">How to execute a shell script on a Remote Node using Ansible?</a></li>
  </ul>
  <p><strong>Further Recommendation</strong></p>
  <p>Instead of using the <code>shell</code> module implement the commands with Ansible modules</p>
  <ul>
  <li><a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html" rel="nofollow noreferrer"><code>file</code> module – Manage files and file properties</a></li>
  <li><a href="https://stackoverflow.com/questions/71585303/">How can I manage keyring files in trusted.gpg.d within an Ansible playbook?</a></li>
  </ul>
  
Answers: CC BY-SA 4.0
Answers: 2023-02-07T11:07:45.103Z
Answers: 2023-02-07T11:28:33.867Z
Answers: 2023-02-07T11:28:33.867Z
Answers: U880D
Body: <p>For some reason this fails:</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: &quot;Get GPG key from the Jenkins portal&quot;
    get_url:
      url: https://pkg.jenkins.io/debian-stable/jenkins.io.key
      dest: /usr/share/keyrings/jenkins.io.key
  - name: &quot;Delete existing keys and de-arm&quot;
    become: true
    shell: |
       &quot;rm -rf /usr/share/keyrings/jenkins.gpg&quot;
       &quot;gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key&quot;
  </code></pre>
  <p>This is a part of a tasks file that gets called from another file. I do not see why the first should fail. The failure reason is:</p>
  <pre class="lang-json prettyprint-override"><code>fatal: [localhost]: FAILED! =&gt; {
      &quot;changed&quot;: true,
      &quot;cmd&quot;: &quot;\&quot;rm -rf /usr/share/keyrings/jenkins.gpg\&quot;\n\&quot;gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key\&quot;\n&quot;,
      &quot;delta&quot;: &quot;0:00:00.021351&quot;,
      &quot;end&quot;: &quot;2023-02-07 12:00:17.975367&quot;,
      &quot;invocation&quot;: {
          &quot;module_args&quot;: {
              &quot;_raw_params&quot;: &quot;\&quot;rm -rf /usr/share/keyrings/jenkins.gpg\&quot;\n\&quot;gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key\&quot;\n&quot;,
              &quot;_uses_shell&quot;: true,
              &quot;argv&quot;: null,
              &quot;chdir&quot;: null,
              &quot;creates&quot;: null,
              &quot;executable&quot;: null,
              &quot;removes&quot;: null,
              &quot;stdin&quot;: null,
              &quot;stdin_add_newline&quot;: true,
              &quot;strip_empty_ends&quot;: true
          }
      },
      &quot;msg&quot;: &quot;non-zero return code&quot;,
      &quot;rc&quot;: 127,
      &quot;start&quot;: &quot;2023-02-07 12:00:17.954016&quot;,
      &quot;stderr&quot;: &quot;/bin/sh: 1: rm -rf /usr/share/keyrings/jenkins.gpg: not found\n/bin/sh: 2: gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key: not found&quot;,
      &quot;stderr_lines&quot;: [
          &quot;/bin/sh: 1: rm -rf /usr/share/keyrings/jenkins.gpg: not found&quot;,
          &quot;/bin/sh: 2: gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key: not found&quot;
      ],
      &quot;stdout&quot;: &quot;&quot;,
      &quot;stdout_lines&quot;: []
  }
  </code></pre>
  <p>I don't know what <code>not found</code> means in this context. I can run these very commands in sequence on a real shell and they work.</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: &quot;Get GPG key from the Jenkins portal&quot;
    get_url:
      url: https://pkg.jenkins.io/debian-stable/jenkins.io.key
      dest: /usr/share/keyrings/jenkins.io.key
  - name: &quot;Delete existing key&quot;
    become: true
    shell: &quot;rm -rf /usr/share/keyrings/jenkins.gpg&quot;
  - name: &quot;De-arm the keys&quot;
    become: true
    shell: &quot;gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key&quot;
    register: jenkinskey
  </code></pre>
  <p>Since these get processed via the shell, they should be fine I believe.</p>
  <p>Any hints?</p>
  
Tags: <ansible>
Title: Multiple shell commands not executing with shell module
UserDisplayName: Skegg
ThreadText: # Multiple shell commands not executing with shell module
  
  (Asked by: username_0 on 2023-02-07)
  
  For some reason this fails:
  
  ```yaml
  - name: "Get GPG key from the Jenkins portal"
    get_url:
      url: https://pkg.jenkins.io/debian-stable/jenkins.io.key
      dest: /usr/share/keyrings/jenkins.io.key
  - name: "Delete existing keys and de-arm"
    become: true
    shell: |
       "rm -rf /usr/share/keyrings/jenkins.gpg"
       "gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key"
  
  ```
  
  This is a part of a tasks file that gets called from another file. I do not see why the first should fail. The failure reason is:
  
  ```json
  fatal: [localhost]: FAILED! => {
      "changed": true,
      "cmd": "\"rm -rf /usr/share/keyrings/jenkins.gpg\"\n\"gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key\"\n",
      "delta": "0:00:00.021351",
      "end": "2023-02-07 12:00:17.975367",
      "invocation": {
          "module_args": {
              "_raw_params": "\"rm -rf /usr/share/keyrings/jenkins.gpg\"\n\"gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key\"\n",
              "_uses_shell": true,
              "argv": null,
              "chdir": null,
              "creates": null,
              "executable": null,
              "removes": null,
              "stdin": null,
              "stdin_add_newline": true,
              "strip_empty_ends": true
          }
      },
      "msg": "non-zero return code",
      "rc": 127,
      "start": "2023-02-07 12:00:17.954016",
      "stderr": "/bin/sh: 1: rm -rf /usr/share/keyrings/jenkins.gpg: not found\n/bin/sh: 2: gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key: not found",
      "stderr_lines": [
          "/bin/sh: 1: rm -rf /usr/share/keyrings/jenkins.gpg: not found",
          "/bin/sh: 2: gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key: not found"
      ],
      "stdout": "",
      "stdout_lines": []
  }
  
  ```
  
  I don't know what `not found` means in this context. I can run these very commands in sequence on a real shell and they work.
  
  ```yaml
  - name: "Get GPG key from the Jenkins portal"
    get_url:
      url: https://pkg.jenkins.io/debian-stable/jenkins.io.key
      dest: /usr/share/keyrings/jenkins.io.key
  - name: "Delete existing key"
    become: true
    shell: "rm -rf /usr/share/keyrings/jenkins.gpg"
  - name: "De-arm the keys"
    become: true
    shell: "gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key"
    register: jenkinskey
  
  ```
  
  Since these get processed via the shell, they should be fine I believe.
  
  Any hints?
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-02-07. Score: 1 | Accepted answer
  
  > *I do not see why the first should fail.*
  
  It looks like the task has a syntax error
  
  ```yaml
  - name: "Delete existing keys and de-arm"
    become: true
    shell: |
       "rm -rf /usr/share/keyrings/jenkins.gpg"
       "gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key"
  
  ```
  
  According the error message
  
  ```yaml
      "stderr": "/bin/sh: 1: rm -rf /usr/share/keyrings/jenkins.gpg: not found\n/bin/sh: 2: gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key: not found",
      "stderr_lines": [
          "/bin/sh: 1: rm -rf /usr/share/keyrings/jenkins.gpg: not found",
          "/bin/sh: 2: gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key: not found"
  
  ```
  
  The lines are processed as one single command and if I interpret it correctly. Furthermore, no quotes are necessary.
  
  > *I don't know what `not found` means in this context.*
  
  It means that the command as a whole or parts of it wasn't found, a syntax error and is the expected behavior.
  
  **Possible Solution**
  
  You could try with
  
  ```yaml
    - name: Exec sh script on Remote Node
      shell:
        cmd: |
          rm -f /usr/share/keyrings/jenkins.gpg
          gpg --dearmor -o /usr/share/keyrings/jenkins.gpg /usr/share/keyrings/jenkins.io.key
      register: result
  
  ```
  
  or even with
  
  ```yaml
      shell: "rm -f /usr/share/keyrings/jenkins.gpg && gpg --dearmor -o /usr/share/keyrings/jenkins.gpg"
  
  ```
  
  **Similar Q&A**
  
  * [How to execute a shell script on a Remote Node using Ansible?](https://stackoverflow.com/a/73594837/6771046)
  
  **Further Recommendation**
  
  Instead of using the `shell` module implement the commands with Ansible modules
  
  * [`file` module – Manage files and file properties](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html)
  * [How can I manage keyring files in trusted.gpg.d within an Ansible playbook?](https://stackoverflow.com/questions/71585303/)
  
  
  ---
  
  