Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to filter out a line out of Ansible stdout_lines result. The ansible playbook task I ran was the following shell argument:

- name: VERIFY | Confirm that queue exists properly
  shell: aws sqs list-queues --region {{region}}
  environment: "{{ aws_cli_environment|d({}) }}"
  register: sqs_list

To see the results, I followed it up with a debug:

- debug:
    msg: "{{ sqs_list.stdout_lines|list }}"

which gives the following result during the playbook run:

TASK [sqs : debug] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "    \"QueueUrls\": [",
        "        \"sampleurl1\",",
        "        \"sampleurl2\",",
        "        \"sampleurl3\",",
        "        \"sampleurl4\",",
        "        \"https://sampleurl5/test_sqs\"",
        "    ]",

I want ONLY the last url, "test_sqs" to appear under "msg":, I tried the following filter but had no luck:

- name: VERIFY | Find SQS url
  command: "{{ sqs_list.stdout_lines|list }} -- formats | grep $sqs"
PLAY [localhost] **********************************
TASK [command] ************************************
changed: [localhost]
TASK [debug] **************************************
ok: [localhost] => {
    "msg": "https://eu-west-1.queue.amazonaws.com/xxxxx/test"
                Thanks, I'm still getting an error trying to modify this example due to the comma character at the beginning of the json.  - debug:        msg: "{{ (sqs_list.stdout_lines|from_json).'{'.QueueUrls|last }}" or - debug:        msg: "{{ (sqs_list.stdout_lines|from_json).','.QueueUrls|last }}"  fatal: [localhost]: FAILED! => {"failed": true, "msg": "template error while templating string: expected name or number. String: {{ (sqs_list.stdout_lines|from_json).'{'.QueueUrls|last }}"}
– J. Patwary
                Oct 25, 2017 at 16:52
                I've provided complete working example. Please check your tooling. JSON output from aws should be valid.
– Konstantin Suvorov
                Oct 25, 2017 at 17:06
  tasks:
    - name: list queue
      shell: aws sqs list-queues --region us-east-1 --queue-name-prefix test_sqs --query 'QueueUrls[*]' --output text
      register: sqs_list
    - debug: 
        msg: "{{ sqs_list.stdout_lines|list }}"

Output:

ok: [localhost] => {
    "msg": [
        "https://queue.amazonaws.com/xxxxxxxxx/test_sqs-198O8HG46WK1Z"

You can make use of the --queue-name-prefix parameter to list only queues which is starting with name test_sqs. If there is only one queue with that name, you can use this solution.

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.