Ansible command using variable input fails to execute





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Below I'm trying to get a command running involving input from previous output, in this case copy flash:file01 flash:file02 with 2 derived from the REGEX_FILTERING task.



- name: REGEX_FILTERING
command: "cat /home/ubuntu/show_switch.txt"
register: line1
- debug: var="{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"
register: number

- name: COMMAND_INPUT
cli_command:
command: 'copy flash:file01 flash:file0 {{ number }}'
prompt: '[confirm]'
answer: ''


Here's the error message:



<ommited>
"stdout_lines": [
"Switch/Stack Mac Address : d4a0.2ae9.ec00",
" H/W Current",
"----------------------------------------------------------",
" 1 Member 001f.9df6.2d80 5 0 Ready ",
"*2 Master d4a0.2ae9.ec00 15 0 Ready ",
" 3 Member 0016.c733.cd00 5 0 Ready"
]
}

TASK [debug] *****************************************************************************************************************************************************************************
task path: /home/ubuntu/test1.yml:109
ok: [CSR1] => {
"2": "2"
}

TASK [COMMAND_INPUT] *********************************************************************************************************************************************************************
task path: /home/ubuntu/test1.yml:112
<192.168.255.133> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<192.168.255.133> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" && echo ansible-tmp-1542986468.48-164922344690120="` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" ) && sleep 0'
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/network/cli/cli_command.py
<192.168.255.133> PUT /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/tmpkozHVx TO /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py
<192.168.255.133> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
<192.168.255.133> EXEC /bin/sh -c '/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
<192.168.255.133> EXEC /bin/sh -c 'rm -f -r /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
File "/tmp/ansible_cli_command_payload_U1p385/__main__.py", line 150, in main
response = connection.get(**module.params)
File "/tmp/ansible_cli_command_payload_U1p385/ansible_cli_command_payload.zip/ansible/module_utils/connection.py", line 173, in __rpc__
raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)

fatal: [CSR1]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"answer": [
""
],
"check_all": false,
"command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",
"prompt": [
"[confirm]"
],
"sendonly": false
}
},
"msg": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}rn ^rn% Invalid input detected at '^' marker.rnrnCSR1#rnCSR1#"









share|improve this question































    0















    Below I'm trying to get a command running involving input from previous output, in this case copy flash:file01 flash:file02 with 2 derived from the REGEX_FILTERING task.



    - name: REGEX_FILTERING
    command: "cat /home/ubuntu/show_switch.txt"
    register: line1
    - debug: var="{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"
    register: number

    - name: COMMAND_INPUT
    cli_command:
    command: 'copy flash:file01 flash:file0 {{ number }}'
    prompt: '[confirm]'
    answer: ''


    Here's the error message:



    <ommited>
    "stdout_lines": [
    "Switch/Stack Mac Address : d4a0.2ae9.ec00",
    " H/W Current",
    "----------------------------------------------------------",
    " 1 Member 001f.9df6.2d80 5 0 Ready ",
    "*2 Master d4a0.2ae9.ec00 15 0 Ready ",
    " 3 Member 0016.c733.cd00 5 0 Ready"
    ]
    }

    TASK [debug] *****************************************************************************************************************************************************************************
    task path: /home/ubuntu/test1.yml:109
    ok: [CSR1] => {
    "2": "2"
    }

    TASK [COMMAND_INPUT] *********************************************************************************************************************************************************************
    task path: /home/ubuntu/test1.yml:112
    <192.168.255.133> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
    <192.168.255.133> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" && echo ansible-tmp-1542986468.48-164922344690120="` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" ) && sleep 0'
    Using module file /usr/lib/python2.7/dist-packages/ansible/modules/network/cli/cli_command.py
    <192.168.255.133> PUT /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/tmpkozHVx TO /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py
    <192.168.255.133> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
    <192.168.255.133> EXEC /bin/sh -c '/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
    <192.168.255.133> EXEC /bin/sh -c 'rm -f -r /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ > /dev/null 2>&1 && sleep 0'
    The full traceback is:
    WARNING: The below traceback may *not* be related to the actual failure.
    File "/tmp/ansible_cli_command_payload_U1p385/__main__.py", line 150, in main
    response = connection.get(**module.params)
    File "/tmp/ansible_cli_command_payload_U1p385/ansible_cli_command_payload.zip/ansible/module_utils/connection.py", line 173, in __rpc__
    raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)

    fatal: [CSR1]: FAILED! => {
    "changed": false,
    "invocation": {
    "module_args": {
    "answer": [
    ""
    ],
    "check_all": false,
    "command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",
    "prompt": [
    "[confirm]"
    ],
    "sendonly": false
    }
    },
    "msg": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}rn ^rn% Invalid input detected at '^' marker.rnrnCSR1#rnCSR1#"









    share|improve this question



























      0












      0








      0








      Below I'm trying to get a command running involving input from previous output, in this case copy flash:file01 flash:file02 with 2 derived from the REGEX_FILTERING task.



      - name: REGEX_FILTERING
      command: "cat /home/ubuntu/show_switch.txt"
      register: line1
      - debug: var="{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"
      register: number

      - name: COMMAND_INPUT
      cli_command:
      command: 'copy flash:file01 flash:file0 {{ number }}'
      prompt: '[confirm]'
      answer: ''


      Here's the error message:



      <ommited>
      "stdout_lines": [
      "Switch/Stack Mac Address : d4a0.2ae9.ec00",
      " H/W Current",
      "----------------------------------------------------------",
      " 1 Member 001f.9df6.2d80 5 0 Ready ",
      "*2 Master d4a0.2ae9.ec00 15 0 Ready ",
      " 3 Member 0016.c733.cd00 5 0 Ready"
      ]
      }

      TASK [debug] *****************************************************************************************************************************************************************************
      task path: /home/ubuntu/test1.yml:109
      ok: [CSR1] => {
      "2": "2"
      }

      TASK [COMMAND_INPUT] *********************************************************************************************************************************************************************
      task path: /home/ubuntu/test1.yml:112
      <192.168.255.133> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
      <192.168.255.133> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" && echo ansible-tmp-1542986468.48-164922344690120="` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" ) && sleep 0'
      Using module file /usr/lib/python2.7/dist-packages/ansible/modules/network/cli/cli_command.py
      <192.168.255.133> PUT /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/tmpkozHVx TO /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py
      <192.168.255.133> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
      <192.168.255.133> EXEC /bin/sh -c '/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
      <192.168.255.133> EXEC /bin/sh -c 'rm -f -r /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ > /dev/null 2>&1 && sleep 0'
      The full traceback is:
      WARNING: The below traceback may *not* be related to the actual failure.
      File "/tmp/ansible_cli_command_payload_U1p385/__main__.py", line 150, in main
      response = connection.get(**module.params)
      File "/tmp/ansible_cli_command_payload_U1p385/ansible_cli_command_payload.zip/ansible/module_utils/connection.py", line 173, in __rpc__
      raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)

      fatal: [CSR1]: FAILED! => {
      "changed": false,
      "invocation": {
      "module_args": {
      "answer": [
      ""
      ],
      "check_all": false,
      "command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",
      "prompt": [
      "[confirm]"
      ],
      "sendonly": false
      }
      },
      "msg": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}rn ^rn% Invalid input detected at '^' marker.rnrnCSR1#rnCSR1#"









      share|improve this question
















      Below I'm trying to get a command running involving input from previous output, in this case copy flash:file01 flash:file02 with 2 derived from the REGEX_FILTERING task.



      - name: REGEX_FILTERING
      command: "cat /home/ubuntu/show_switch.txt"
      register: line1
      - debug: var="{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"
      register: number

      - name: COMMAND_INPUT
      cli_command:
      command: 'copy flash:file01 flash:file0 {{ number }}'
      prompt: '[confirm]'
      answer: ''


      Here's the error message:



      <ommited>
      "stdout_lines": [
      "Switch/Stack Mac Address : d4a0.2ae9.ec00",
      " H/W Current",
      "----------------------------------------------------------",
      " 1 Member 001f.9df6.2d80 5 0 Ready ",
      "*2 Master d4a0.2ae9.ec00 15 0 Ready ",
      " 3 Member 0016.c733.cd00 5 0 Ready"
      ]
      }

      TASK [debug] *****************************************************************************************************************************************************************************
      task path: /home/ubuntu/test1.yml:109
      ok: [CSR1] => {
      "2": "2"
      }

      TASK [COMMAND_INPUT] *********************************************************************************************************************************************************************
      task path: /home/ubuntu/test1.yml:112
      <192.168.255.133> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
      <192.168.255.133> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" && echo ansible-tmp-1542986468.48-164922344690120="` echo /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120 `" ) && sleep 0'
      Using module file /usr/lib/python2.7/dist-packages/ansible/modules/network/cli/cli_command.py
      <192.168.255.133> PUT /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/tmpkozHVx TO /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py
      <192.168.255.133> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
      <192.168.255.133> EXEC /bin/sh -c '/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/AnsiballZ_cli_command.py && sleep 0'
      <192.168.255.133> EXEC /bin/sh -c 'rm -f -r /home/ubuntu/.ansible/tmp/ansible-local-7528uUJSLh/ansible-tmp-1542986468.48-164922344690120/ > /dev/null 2>&1 && sleep 0'
      The full traceback is:
      WARNING: The below traceback may *not* be related to the actual failure.
      File "/tmp/ansible_cli_command_payload_U1p385/__main__.py", line 150, in main
      response = connection.get(**module.params)
      File "/tmp/ansible_cli_command_payload_U1p385/ansible_cli_command_payload.zip/ansible/module_utils/connection.py", line 173, in __rpc__
      raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)

      fatal: [CSR1]: FAILED! => {
      "changed": false,
      "invocation": {
      "module_args": {
      "answer": [
      ""
      ],
      "check_all": false,
      "command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",
      "prompt": [
      "[confirm]"
      ],
      "sendonly": false
      }
      },
      "msg": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}rn ^rn% Invalid input detected at '^' marker.rnrnCSR1#rnCSR1#"






      ansible






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 17:26









      Mihai Chelaru

      2,485101424




      2,485101424










      asked Nov 23 '18 at 14:07









      Dan SabottDan Sabott

      33




      33
























          1 Answer
          1






          active

          oldest

          votes


















          0















          "command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",




          Is happening because the register: number is not in fact the number, rather it's the ansible result of the debug: output. Since number is a python dict, when it is injected into your command, it is just serialized as if str() were called upon the dict



          It is far more likely that what you want is to capture that variable locally in the task, to make it just be the result of the jinja2 expression, and not the result of an ansible task:



          - cli_command:
          command: 'copy flash:file01 flash:file0 {{ number }}'
          vars:
          number: "{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"


          I also suspect you will be happier with line1.stdout, which is a str of the entire output, versus line1.stdout_lines which is a list of str, making the regex_replace on a list kind of weird -- I'm surprised it actually even let you do that, to be honest






          share|improve this answer
























          • Thanks Matthew! I tried it and some syntax error occurred. - name: COMMAND_INPUT ios_command: commands: command: 'copy flash:file01 flash:file0 {{ apple }}' prompt: '[confirm]' answer: '' vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}"

            – Dan Sabott
            Nov 29 '18 at 9:08













          • ERROR! Syntax Error while loading YAML. found unknown escape character The error appears to have been in '/home/ubuntu/test1.yml': line 119, column 54, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}" ^ here

            – Dan Sabott
            Nov 29 '18 at 9:13











          • I would bet it's because of the backslash inside double-quotes, in which yaml will expand backslashes; you can use regex "character classes" to effectively quote the asterisk without getting into the backslash business: regex_replace('(?<![*]).?') type deal

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • In the future, have some decency and use code formatting for your code snippets

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • Perfect! It works now. Appreciate your help! And got your point with formatting, I will do.

            – Dan Sabott
            Dec 5 '18 at 10:07












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448182%2fansible-command-using-variable-input-fails-to-execute%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0















          "command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",




          Is happening because the register: number is not in fact the number, rather it's the ansible result of the debug: output. Since number is a python dict, when it is injected into your command, it is just serialized as if str() were called upon the dict



          It is far more likely that what you want is to capture that variable locally in the task, to make it just be the result of the jinja2 expression, and not the result of an ansible task:



          - cli_command:
          command: 'copy flash:file01 flash:file0 {{ number }}'
          vars:
          number: "{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"


          I also suspect you will be happier with line1.stdout, which is a str of the entire output, versus line1.stdout_lines which is a list of str, making the regex_replace on a list kind of weird -- I'm surprised it actually even let you do that, to be honest






          share|improve this answer
























          • Thanks Matthew! I tried it and some syntax error occurred. - name: COMMAND_INPUT ios_command: commands: command: 'copy flash:file01 flash:file0 {{ apple }}' prompt: '[confirm]' answer: '' vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}"

            – Dan Sabott
            Nov 29 '18 at 9:08













          • ERROR! Syntax Error while loading YAML. found unknown escape character The error appears to have been in '/home/ubuntu/test1.yml': line 119, column 54, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}" ^ here

            – Dan Sabott
            Nov 29 '18 at 9:13











          • I would bet it's because of the backslash inside double-quotes, in which yaml will expand backslashes; you can use regex "character classes" to effectively quote the asterisk without getting into the backslash business: regex_replace('(?<![*]).?') type deal

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • In the future, have some decency and use code formatting for your code snippets

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • Perfect! It works now. Appreciate your help! And got your point with formatting, I will do.

            – Dan Sabott
            Dec 5 '18 at 10:07
















          0















          "command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",




          Is happening because the register: number is not in fact the number, rather it's the ansible result of the debug: output. Since number is a python dict, when it is injected into your command, it is just serialized as if str() were called upon the dict



          It is far more likely that what you want is to capture that variable locally in the task, to make it just be the result of the jinja2 expression, and not the result of an ansible task:



          - cli_command:
          command: 'copy flash:file01 flash:file0 {{ number }}'
          vars:
          number: "{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"


          I also suspect you will be happier with line1.stdout, which is a str of the entire output, versus line1.stdout_lines which is a list of str, making the regex_replace on a list kind of weird -- I'm surprised it actually even let you do that, to be honest






          share|improve this answer
























          • Thanks Matthew! I tried it and some syntax error occurred. - name: COMMAND_INPUT ios_command: commands: command: 'copy flash:file01 flash:file0 {{ apple }}' prompt: '[confirm]' answer: '' vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}"

            – Dan Sabott
            Nov 29 '18 at 9:08













          • ERROR! Syntax Error while loading YAML. found unknown escape character The error appears to have been in '/home/ubuntu/test1.yml': line 119, column 54, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}" ^ here

            – Dan Sabott
            Nov 29 '18 at 9:13











          • I would bet it's because of the backslash inside double-quotes, in which yaml will expand backslashes; you can use regex "character classes" to effectively quote the asterisk without getting into the backslash business: regex_replace('(?<![*]).?') type deal

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • In the future, have some decency and use code formatting for your code snippets

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • Perfect! It works now. Appreciate your help! And got your point with formatting, I will do.

            – Dan Sabott
            Dec 5 '18 at 10:07














          0












          0








          0








          "command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",




          Is happening because the register: number is not in fact the number, rather it's the ansible result of the debug: output. Since number is a python dict, when it is injected into your command, it is just serialized as if str() were called upon the dict



          It is far more likely that what you want is to capture that variable locally in the task, to make it just be the result of the jinja2 expression, and not the result of an ansible task:



          - cli_command:
          command: 'copy flash:file01 flash:file0 {{ number }}'
          vars:
          number: "{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"


          I also suspect you will be happier with line1.stdout, which is a str of the entire output, versus line1.stdout_lines which is a list of str, making the regex_replace on a list kind of weird -- I'm surprised it actually even let you do that, to be honest






          share|improve this answer














          "command": "copy flash:file01 flash:file0 {'failed': False, u'2': u'2', 'changed': False}",




          Is happening because the register: number is not in fact the number, rather it's the ansible result of the debug: output. Since number is a python dict, when it is injected into your command, it is just serialized as if str() were called upon the dict



          It is far more likely that what you want is to capture that variable locally in the task, to make it just be the result of the jinja2 expression, and not the result of an ansible task:



          - cli_command:
          command: 'copy flash:file01 flash:file0 {{ number }}'
          vars:
          number: "{{ line1.stdout_lines | regex_replace('(?<!*).?') }}"


          I also suspect you will be happier with line1.stdout, which is a str of the entire output, versus line1.stdout_lines which is a list of str, making the regex_replace on a list kind of weird -- I'm surprised it actually even let you do that, to be honest







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 18:11









          Matthew L DanielMatthew L Daniel

          9,69112729




          9,69112729













          • Thanks Matthew! I tried it and some syntax error occurred. - name: COMMAND_INPUT ios_command: commands: command: 'copy flash:file01 flash:file0 {{ apple }}' prompt: '[confirm]' answer: '' vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}"

            – Dan Sabott
            Nov 29 '18 at 9:08













          • ERROR! Syntax Error while loading YAML. found unknown escape character The error appears to have been in '/home/ubuntu/test1.yml': line 119, column 54, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}" ^ here

            – Dan Sabott
            Nov 29 '18 at 9:13











          • I would bet it's because of the backslash inside double-quotes, in which yaml will expand backslashes; you can use regex "character classes" to effectively quote the asterisk without getting into the backslash business: regex_replace('(?<![*]).?') type deal

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • In the future, have some decency and use code formatting for your code snippets

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • Perfect! It works now. Appreciate your help! And got your point with formatting, I will do.

            – Dan Sabott
            Dec 5 '18 at 10:07



















          • Thanks Matthew! I tried it and some syntax error occurred. - name: COMMAND_INPUT ios_command: commands: command: 'copy flash:file01 flash:file0 {{ apple }}' prompt: '[confirm]' answer: '' vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}"

            – Dan Sabott
            Nov 29 '18 at 9:08













          • ERROR! Syntax Error while loading YAML. found unknown escape character The error appears to have been in '/home/ubuntu/test1.yml': line 119, column 54, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}" ^ here

            – Dan Sabott
            Nov 29 '18 at 9:13











          • I would bet it's because of the backslash inside double-quotes, in which yaml will expand backslashes; you can use regex "character classes" to effectively quote the asterisk without getting into the backslash business: regex_replace('(?<![*]).?') type deal

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • In the future, have some decency and use code formatting for your code snippets

            – Matthew L Daniel
            Nov 30 '18 at 5:25











          • Perfect! It works now. Appreciate your help! And got your point with formatting, I will do.

            – Dan Sabott
            Dec 5 '18 at 10:07

















          Thanks Matthew! I tried it and some syntax error occurred. - name: COMMAND_INPUT ios_command: commands: command: 'copy flash:file01 flash:file0 {{ apple }}' prompt: '[confirm]' answer: '' vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}"

          – Dan Sabott
          Nov 29 '18 at 9:08







          Thanks Matthew! I tried it and some syntax error occurred. - name: COMMAND_INPUT ios_command: commands: command: 'copy flash:file01 flash:file0 {{ apple }}' prompt: '[confirm]' answer: '' vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}"

          – Dan Sabott
          Nov 29 '18 at 9:08















          ERROR! Syntax Error while loading YAML. found unknown escape character The error appears to have been in '/home/ubuntu/test1.yml': line 119, column 54, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}" ^ here

          – Dan Sabott
          Nov 29 '18 at 9:13





          ERROR! Syntax Error while loading YAML. found unknown escape character The error appears to have been in '/home/ubuntu/test1.yml': line 119, column 54, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: vars: apple: "{{ line1.stdout | regex_replace('(?<!*).?') }}" ^ here

          – Dan Sabott
          Nov 29 '18 at 9:13













          I would bet it's because of the backslash inside double-quotes, in which yaml will expand backslashes; you can use regex "character classes" to effectively quote the asterisk without getting into the backslash business: regex_replace('(?<![*]).?') type deal

          – Matthew L Daniel
          Nov 30 '18 at 5:25





          I would bet it's because of the backslash inside double-quotes, in which yaml will expand backslashes; you can use regex "character classes" to effectively quote the asterisk without getting into the backslash business: regex_replace('(?<![*]).?') type deal

          – Matthew L Daniel
          Nov 30 '18 at 5:25













          In the future, have some decency and use code formatting for your code snippets

          – Matthew L Daniel
          Nov 30 '18 at 5:25





          In the future, have some decency and use code formatting for your code snippets

          – Matthew L Daniel
          Nov 30 '18 at 5:25













          Perfect! It works now. Appreciate your help! And got your point with formatting, I will do.

          – Dan Sabott
          Dec 5 '18 at 10:07





          Perfect! It works now. Appreciate your help! And got your point with formatting, I will do.

          – Dan Sabott
          Dec 5 '18 at 10:07




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448182%2fansible-command-using-variable-input-fails-to-execute%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown