This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.

⚠️ We've moved!

Hi there!

To reduce project dependency on 3rd party paid services the StackStorm TSC has decided to move the Q/A from this forum to Github Discussions. This will make user experience better integrated with the native Github flow, as well as the questions closer to the community where they can provide answers.

Use 🔗 Github Discussions to ask your questions.

Question regarding reiteration over napalm.cli workflow action

I’ve been digging around the forums and docs for a bit but haven’t come up with a good solution for this yet aside from replacing an action.

I’ve got the following workflow currently able to request very basic approval, and then push commands to a single host via the napalm.cli pack action. However, when using a different netbox inventory output, it’s not reiterating over the multiple names pushed in output, but doing the first and stopping.

I thought it’d be a quick “with items” fix to get the task to repeat over resulting hosts however the napalm.cli action does not support “with items” and causes an error stoppage. I can’t think of a decent method around this aside from replacing the napalm.cli task with an ansible playbook doing the legwork in it’s place.

If there’s another way to do this that I’m looking over - any push to a doc/kb article or assistance would be great!

Here’s my workflow being used, debug output of the “json” output parameter is simply a list of hostnames in string format for the action input.

#version: 1.0

input:
  - site
  - tag
  - commands
  
tasks:
  # [116, 137]
  get_approval:
    action: core.ask
    input:
      schema:
        type: object
        properties:
          approved:
            type: boolean
            description: "Do you have Approval?"
            required: True
          ticket_id:
            type: string
            description: "Ticket (CR/IR) ID:"
            required: True
      ttl: 90
    next:
      - when: <% task(get_approval).result.response.approved = true %>
        publish:
          - approver_department_id: <% task(get_approval).result.response.ticket_id %>
        do: get_inventory
      - when: <% task(get_approval).result.response.approved = false %>
        do: fail
  # [116, 314]
  get_inventory:
    action: netbox.get.dcim.devices
    next:
      - publish:
          - stdout: <% result().exit_code %>
          - stderr: <% result().stderr %>
          - json: <% result().result.raw.results.name %>
        do:
          - push_commands
    input:
      log_level: DEBUG
      site: <% ctx().site %>
      tag: <% ctx().tag %>
      save_in_key_store: false
  # [116, 493]
  push_commands:
    action: napalm.cli
    input:
      log_level: DEBUG
      commands:
        - <% ctx().commands %>
      hostname: <% ctx().json[0] %>
      driver: junos
    next:
      - do:
          - notify
  # [116, 629]
  notify:
    action: ansible.playbook
    input:
      cwd: {{ Redacted }}/notify/
      playbook: main.yml
      extra_vars:
        - '@extras/napalm_nbx_success.json'
 # remove success.json and get actual input next (low priority)

That line is selecting just the first entry in the JSON list of hosts so it will only run the action the single time.

You were correct that it should need the “with:items” setup to iterate over your list of hosts and run the command on all. I think the task will need to look something like the following:

push_commands:
    with:
      items: <% ctx().json %>
      concurrency: 10
    action: napalm.cli
    input:
      log_level: DEBUG
      commands:
        - <% ctx().commands %>
      hostname: <% item() %>
      driver: junos
    next:
      - do:
          - notify
  # [116, 629]
  notify:
    action: ansible.playbook
    input:
      cwd: {{ Redacted }}/notify/
      playbook: main.yml
      extra_vars:
        - '@extras/napalm_nbx_success.json'
 # remove success.json and get actual input next (low priority)

I copied and paste your text so the indentation may be off but the commands and variables should be correct so YMMV. Also, with the concurrency option set to 10 will run 10 actions at a time and may need to be tweaked for your environment.

1 Like

Thanks for the help!

Your answer got it working immediately. Comparing a backup of the workflow to yours, I had placed the with:items in an incorrect spot within the action (placed it at the bottom just above “next -do” and I guess it was parsing improperly.

Sorry for leaving in the [0], that was from a test pushing to just one device (and forcing it to stay that way). I didn’t catch that I left it there.