ansible / ansible

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.
https://www.ansible.com/
GNU General Public License v3.0
61.93k stars 23.77k forks source link

stdout_lines is returning [uoutput instead of only output #11535

Closed esfreire closed 7 years ago

esfreire commented 9 years ago
ISSUE TYPE

Bug Report

COMPONENT NAME

N/A

ANSIBLE VERSION

N/A

SUMMARY

Hi all,

I don't know if it is a bug but I don't know how to solve it. I also have asked about it on the ansible group, https://groups.google.com/forum/#!topic/ansible-project/x2MCN2Qc4GU

I have the two following actions:

- name: Getting the IDs for the VMs running under the user
  sudo: True
  sudo_user: oneadmin
  shell: onevm list | grep {{ item.value.login_name }} |  awk 'BEGIN {FS=" "}{print $1}'
  with_dict: disable_users_account
  register: VMIDs_per_user
  ignore_errors: yes
  tags:
    - delete_VMs

- name: Delete the VMs running under the user
  sudo: True
  sudo_user: oneadmin
  command: onevm delete {{ item.stdout }}
  with_items: VMIDs_per_user.results
  tags:
    - delete_VMs

This is an output example from the the first action:

674
675

The problem is that the second action is not working due to the command expects to received just one argument per time, I mean, I need to execute:

onevm delete 674

onevm delete 675

And right now it is not working because it is executing: onevm delete 674 675 , which it does not work because the command does not admit to pass more than one argument.

I followed ansible documentation, https://www.google.com/url?q=https%3A%2F%2Fdocs.ansible.com%2Fplaybooks_loops.html%23iterating-over-the-results-of-a-program-execution&sa=D&sntz=1&usg=AFQjCNHSNuqa9qHFurH5UGrY868zIcz8vA

So I tried

command: onevm delete {{ item }}
with_items: "{{VMIDs_per_user.stdout_lines}}"

But I got the following issue:

TASK: [ON4_project_managment | Delete the VMs running under the user] *********
fatal: [m-opennebula4] => with_items expects a list or a set

Then, I have tried the following things:

shell: onevm delete {{ item.stdout_lines }}
shell: onevm delete {{ item.stdout.split('\n') }}    
shell: onevm delete "{{ item.stdout.split('\n') }}"
shell: onevm delete {{ item.stdout.split("\n") }}
shell: onevm delete {{ item.stdout.splitlines() }}
with_items: VMIDs_per_user.results

But I got the following issue in all the previous cases:

failed: [m-opennebula4] => (item={u'cmd': u'onevm list | grep testing-admin | awk \'BEGIN {FS=" "}{print $1}\'', u'end': u'2015-07-08 15:26:43.028904', u'stderr': u'', u'stdout': u'685\n686', u'changed': True, u'rc': 0, 'item': {'value': {'login_name': 'testing-admin'}, 'key': 'user'}, u'warnings': [], u'delta': u'0:00:00.778189', 'invocation': {'module_name': u'shell', 'module_args': u'onevm list | grep testing-admin | awk \'BEGIN {FS=" "}{print $1}\''}, 'stdout_lines': [u'685', u'686'], u'start': u'2015-07-08 15:26:42.250715'}) => {"changed": true, "cmd": "onevm delete [u'685', u'686']", "delta": "0:00:01.095980", "end": "2015-07-08 15:26:44.508902", "item": {"changed": true, "cmd": "onevm list | grep testing-admin | awk 'BEGIN {FS=\" \"}{print $1}'", "delta": "0:00:00.778189", "end": "2015-07-08 15:26:43.028904", "invocation": {"module_args": "onevm list | grep testing-admin | awk 'BEGIN {FS=\" \"}{print $1}'", "module_name": "shell"}, "item": {"key": "user", "value": {"login_name": "testing-admin"}}, "rc": 0, "start": "2015-07-08 15:26:42.250715", "stderr": "", "stdout": "685\n686", "stdout_lines": ["685", "686"], "warnings": []}, "rc": 255, "start": "2015-07-08 15:26:43.412922", "warnings": []}
stdout: VM named [u685 not found.
command delete: argument 0 must be one of range, vmid_list

So, it seems it is splitting the lines according to stdout_lines': [u'685', u'686'] and "stdout": "685\n686", "stdout_lines": ["685", "686"] . The problem is that it is taking the [" characteres as "[u" and I don't know how to remove that.

Any idea is welcome and my apoligize if this is not a bug :)

Thanks in advance, Esteban

santureddy commented 8 years ago

Any luck with this Esteban, i'm using ansible 2.0.0.2 , but couldn't find a solution.

adelino-sdsu commented 7 years ago

I can confirm the same issue in 2.2.0.0

adelino-sdsu commented 7 years ago

Alright I got around this by creating an extra step using set_fact. Rather than running my commands against the std.out, I run my commands against the new variable which also utilizes regex_replace

name: run script script: /etc/ansible/test.sh register: results

name: create variable out of stdout results. then remove the extra characters set_fact: somevariable: "{{ results.stdout | regex_replace('(\r\n)','') }}"

name: echo the variable without the extraneous characters shell: echo "{{ somevariable }}"

jctanner commented 7 years ago

@esfreire can you confirm if the workaround from @oniled helps?

needs_info

esfreire commented 7 years ago

Hi @santureddy , @jctanner and @oniled ,

I am sorry for my delay in answering you.

@oniled , thanks for checking it and for the workaround :)

@santureddy , @jctanner , at the end, I decided to change it and do everything through bash on the shell line since I did not know how to solve this. I am sorry but I cannot test this right now since I am not working on this project/company for almost a year now.

Regards, Esteban

jctanner commented 7 years ago

I don't know the exact output of the onevm commands, but this pattern is covered in the current docs:

http://docs.ansible.com/ansible/playbooks_loops.html#iterating-over-the-results-of-a-program-execution

I ran a quick testcase to validate:

- hosts: el6host
  connection: local
  gather_facts: False
  vars:
      alist:
          - one
          - two
          - three
  tasks:
    - shell: echo "{{ item }}"
      with_items: "{{ alist }}"

Results ...

TASK [command] ***********************************************************************************************************************************************************************************************************************************************
changed: [el6host] => (item=one) => {"changed": true, "cmd": "echo \"one\"", "delta": "0:00:00.002721", "end": "2017-02-10 11:15:10.206580", "item": "one", "rc": 0, "start": "2017-02-10 11:15:10.203859", "stderr": "", "stderr_lines": [], "stdout": "one", "stdout_lines": ["one"]}
changed: [el6host] => (item=two) => {"changed": true, "cmd": "echo \"two\"", "delta": "0:00:00.002939", "end": "2017-02-10 11:15:10.362759", "item": "two", "rc": 0, "start": "2017-02-10 11:15:10.359820", "stderr": "", "stderr_lines": [], "stdout": "two", "stdout_lines": ["two"]}
changed: [el6host] => (item=three) => {"changed": true, "cmd": "echo \"three\"", "delta": "0:00:00.002696", "end": "2017-02-10 11:15:10.521178", "item": "three", "rc": 0, "start": "2017-02-10 11:15:10.518482", "stderr": "", "stderr_lines": [], "stdout": "three", "stdout_lines": ["three"]}