Playbook ansible

Good morning, I have a problem with a task, I want to run a playbook that executes different commands on different machines in my inventory, how can I do it?

There are quiet a few different ways this could be achieved.
A quick and dirty way you could achieve this would be to store the per-host command in host_vars, and then execute the command as a task.

# File: host_vars/your_host/vars
per_host_command: "echo 'Hello, World!'"
[... Playbook Stuff ...]
  tasks:
    - name: Run Custom Command
      ansible.builtin.command: "{{ per_host_command }}"

I’m sure there are much better ways to achieve what you are attempting, but without more information its hard to give a better solution.

2 Likes

I have a list of servers, these servers must download the services at the same time, but the services are stored in a different route so I have not been able to find a way in which I can run within the same playbook different routes on different servers

That may be specific enough for someone who already knows what you mean, but it’s very hand-wavey to me. Can you pick a couple of example machines and show us (me) what you need to do differently on each one? I just need something concrete enough to get some clue what’s actually involved.

1 Like

Per the “Get help” guidelines:

  • Give a clear explanation (and steps to reproduce the issue where possible)
  • Include the version(s) of the relevant software tools, libraries, collections etc
  • Include relavant logs from the issue
  • Use backticks (```) to help format your logs and code (easy-to-read posts are easier to answer too!), like this:
- my code!
  • Set at least one tag (the experts follow the tags, so the right people will find you if you tag)
1 Like

Just to help me understand, let’s pretend they are all exactly the same. Show me what your playbook task would look like to “download the services”.

1 Like