Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run ansible tasks based on machine names in a group

Tags:

ansible

I want to create symlinks for a file using ansible only when I have a certain machine hostname. I know inventory_hostname will give me the hostname, but can I do something like: when: inventory_hostname in group['machines'] so I can run this with all machines in that group? Then, I want to symlink based on name of machine. So:

file:
    src: {{ ansible_hostname }}.png
    dest: anything.png
like image 222
12 revs Avatar asked Jun 04 '18 20:06

12 revs


People also ask

How do I run a specific task in Ansible?

To start executing your playbook at a particular task (usually the task that failed on the previous run), use the --start-at-task option. In this example, Ansible starts executing your playbook at a task named “install packages”.

How can we delegate tasks in Ansible?

Ansible delegate_to property or the keyword specified in the ansible-playbook is used to provide the control to run the task locally or to the other different hosts rather than running on the remote hosts specified on the inventory server list, and this can be the few tasks or running the entire play-book locally and ...

What is parallelism Ansible?

Interact with multiple hosts simultaneously, on a per-playbook basis with Ansible's serial keyword. Posted: June 29, 2022 | 3 min read | by Robert Kimani. ImagebyRoyHarrymanfromPixabay. Parallelism describes a software's ability to spawn multiple processes to execute tasks in tandem.

How do I run a parallel task in Ansible?

If you want to run multiple tasks in a playbook concurrently, use async with poll set to 0. When you set poll: 0 , Ansible starts the task and immediately moves on to the next task without waiting for a result. Each async task runs until it either completes, fails or times out (runs longer than its async value).


1 Answers

You dont need the when: inventory_hostname in group['machines'] condition at all.

You just have to run this task in a play towards hosts: machines, and you will have the symbolic link created to all hosts of the machines group.

UPDATE

if you still want to go for it, and run the playbook towards a big_group but only take action when host is part of the small_group, here is a play that can do it:

hosts file:

[big_group]
greenhat
localhost

[small_group]
localhost

playbook:

---
- hosts: big_group
  # connection: local
  gather_facts: false
  vars:

  tasks:
  - name: print if machine is eligible for symbolic
    debug:
      msg: "machine: {{ inventory_hostname }} is eligible for symbolic link!"
    when: inventory_hostname in groups['small_group']

result:

PLAY [big_group] ****************************************************************************************************************************************************************************************************

TASK [print if machine is eligible for symbolic] ********************************************************************************************************************************************************************
skipping: [greenhat]
ok: [localhost] => {
    "msg": "machine: localhost is eligible for symbolic link!"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************
greenhat                   : ok=0    changed=0    unreachable=0    failed=0   
localhost                  : ok=1    changed=0    unreachable=0    failed=0 

hope it helps

like image 117
ilias-sp Avatar answered Jan 03 '23 19:01

ilias-sp