How to execute command on localhost depending on a remote host

Ainar-G

In my playbook I have this:

- name: compile 
  hosts: localhost
  gather_facts: false
  tasks:
  - name: compile binary
    local_action: command make build FOO=foo1

I want to execute make build FOO=bar1 on localhost once if host is either bar-1 or bar-2 (they're both in group bars, so distinguishing by group is fine too). I tried using when:

  - name: compile binary
    local_action: command make build FOO=foo1
    when: (inventory_hostname != "bar-1") and (inventory_hostname != "bar-2")
  - name: compile binary
    local_action: command make build FOO=bar1
    when: (inventory_hostname == "bar-1") or (inventory_hostname == "bar-2")

But inventory_hostname is always localhost.

In my hosts I have

[foos]
foo-1 ...
foo-2 ...

[bars]
bar-1 ...
bar-2 ...

And I run it as

ansible-playbook -i provision/hosts -l localhost,bars provision/deploy.yml
Zlemini

This task will run a command on localhost once if the current host being operated on is part of the bars group.

   shell: echo {{ inventory_hostname }}
   run_once: true
   delegate_to: localhost
   when: "'bars' in group_names"

Note: If you plan on using serial mode it affects run_once behaviour.

http://docs.ansible.com/ansible/playbooks_delegation.html#run-once

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute command on remote host fails

From Dev

ssh: execute command on the remote host instead of a login shell

From Dev

Execute (Specific) command on remote host without using SSH

From Dev

How to run a command on remote host from a service?

From Dev

How to run a command on remote host from a service?

From Java

How to execute a remote command over ssh with arguments?

From Dev

execute bash command depending on keyword

From Dev

Remote Execute command - No Wait

From Dev

How to remote execute ssh command a sudo command without password

From Dev

Docker, how to remote ssh from command line host to a docker container?

From Dev

How to execute host's Docker command from container?

From Dev

How to execute MySQL command from the host to container running MySQL server?

From Dev

How to execute a command from the host when it's on the $PATH?

From Dev

Bash command substitution on remote host

From Dev

How to execute the mongo cloneCollection command to copy a collection to a remote server

From Dev

How do I execute a command on a remote machine in a golang CLI?

From Dev

How to force ssh to execute command on the remote system instead of locally

From Dev

how can I execute command through ssh, remote is windows

From Dev

Execute a different command depending on the output of the previous

From Dev

Execute a command on Remote Machine in Python

From Dev

Is Jenkins capable to execute remote command?

From Dev

ssh - execute complex remote command

From Dev

Execute remote PS command properly

From Dev

How to host a localhost site on domain?

From Dev

ansible run command on remote host in background

From Dev

MySQL source command contacting remote host

From Dev

Passing a bash command to a remote host using ssh

From Dev

Running a command on the remote host with interactive SSH (conditionally)

From Dev

Running a command on the remote host with interactive SSH (conditionally)

Related Related

  1. 1

    Execute command on remote host fails

  2. 2

    ssh: execute command on the remote host instead of a login shell

  3. 3

    Execute (Specific) command on remote host without using SSH

  4. 4

    How to run a command on remote host from a service?

  5. 5

    How to run a command on remote host from a service?

  6. 6

    How to execute a remote command over ssh with arguments?

  7. 7

    execute bash command depending on keyword

  8. 8

    Remote Execute command - No Wait

  9. 9

    How to remote execute ssh command a sudo command without password

  10. 10

    Docker, how to remote ssh from command line host to a docker container?

  11. 11

    How to execute host's Docker command from container?

  12. 12

    How to execute MySQL command from the host to container running MySQL server?

  13. 13

    How to execute a command from the host when it's on the $PATH?

  14. 14

    Bash command substitution on remote host

  15. 15

    How to execute the mongo cloneCollection command to copy a collection to a remote server

  16. 16

    How do I execute a command on a remote machine in a golang CLI?

  17. 17

    How to force ssh to execute command on the remote system instead of locally

  18. 18

    how can I execute command through ssh, remote is windows

  19. 19

    Execute a different command depending on the output of the previous

  20. 20

    Execute a command on Remote Machine in Python

  21. 21

    Is Jenkins capable to execute remote command?

  22. 22

    ssh - execute complex remote command

  23. 23

    Execute remote PS command properly

  24. 24

    How to host a localhost site on domain?

  25. 25

    ansible run command on remote host in background

  26. 26

    MySQL source command contacting remote host

  27. 27

    Passing a bash command to a remote host using ssh

  28. 28

    Running a command on the remote host with interactive SSH (conditionally)

  29. 29

    Running a command on the remote host with interactive SSH (conditionally)

HotTag

Archive