Register variable, get specific strings and use strings in subsequent task

26 views
Skip to first unread message

Sven

unread,
Sep 28, 2020, 1:44:46 PM9/28/20
to Ansible Project
Hello guys,
I am not quite sure how to describe this.
I have a simple task, which executes a shell command and register a variable.

- name: execute ovs-vsctl show command
  shell: ovs-vsctl show
  register: output

This will give me  stdout with different ports like this:
Port "7da0c92dc [..]"
Port "[479401901[..]"
[...]

But some Ports will have an error:
"could not open network device 5b574[..]"

Now I need to get all the Ports with an error (with a loop i guess (?)). Then execute a command to delete this faulty ports in subsuquent task :

- name: delete faulty ports
  shell: ovs-vsctl del-port 5b574[..]

I was thinking about something like this:

- shell: ' ovs-vsctl del-port "{{item}}" '
  with_items:
    - 5b574 [..]
    -  xxxx [..]

Or what is the best way to realize this? (Probably by using a python/shell script, but it should be done with Ansible)

I have a hard time to realize this.

How can I filter for all those specific strings with faulty ports?
I probably need to save those strings in a list or something, by using a loop, right?

I would be grateful for any help / some ideas regarding the best way to proceed with this.

Dick Visser

unread,
Sep 28, 2020, 3:08:34 PM9/28/20
to ansible...@googlegroups.com
I don't know ovs-vsctl, but
http://www.openvswitch.org/support/dist-docs/ovs-vsctl.8.txt mentions
the option of generating output as JSON for that command.
This will probably simplify your problem.
Can you show some output with that option enabled?
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ef69f9fa-5054-4c67-ae13-f8cfae077647n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Sven

unread,
Sep 28, 2020, 4:15:38 PM9/28/20
to Ansible Project
Thank you for your reply.
I can't rly get a JSON response from the command ovs-vsctl show, since it is only possible for find/list operations.

I tried to get a JSON output using a list operation, but it looks kinda strange to me.. (it shows only a small part, since the output is really long)

{
  "data": [
    [
      [
        "uuid",
        "270cedb4-f43c-4ee1-86f6-d3ac89b449fc"],
      [
        "set",
        []],
      [
        "map",
        []],
      [
        "map",
        []],
      [
        "set",
        []],
      [
        "set",
        []],
      [
        "set",
        []],
      [
        "set",
        []],
      [
        "set",
        []],
      [
        "set",
        []],
      [
        "set",
        []],
      [
        "set",
        []],
      "could not open network device veth1pl4194 (No such device)",
      [
        "map",
     .....
Reply all
Reply to author
Forward
0 new messages