Rajesh Rajendran
Rajesh Rajendran

Reputation: 607

Ansible triggering a task for **specific host** from a role

I want to run a specific task in a playbook to a single host. Is there any better way to do that?

custom playbook

---
- hosts: swarm_manager
  roles:
  - custom_role

roles/custom_role/tasks/main.yml

---
- name: checking for ip
  shell: docker service ps service | grep Running | head -n1 | awk '{print $1}'
  register: ip

- name: killing the container
  **hosts: "{{ip.stdout}}"**
  shell: docker kill $(docker service ps | grep service | awk '{print $1}')

Upvotes: 0

Views: 1469

Answers (1)

imjoseangel
imjoseangel

Reputation: 3936

You can limit doing the following:

---
- name: checking for ip
  shell: docker service ps service | grep Running | head -n1 | awk '{print $1}'
  register: ip

- name: killing the container
  run_once: true
  delegate_to: "{{ip.stdout}}"
  shell: docker kill $(docker service ps | grep service | awk '{print $1}')

I have tested it this way:

---
- hosts: linux
  roles:
  - test_role

And then:

- name: Set Fact
  set_fact:
    ip: 10.100.10.10

- name: Debug
  debug:
    msg: "{{hostvars[groups['linux'][0]]['ansible_default_ipv4']['address']}}"
  run_once: true
  delegate_to: "{{ip}}"

Upvotes: 1

Related Questions