Ansible从角色触发**特定主机**的任务

时间:2018-04-26 08:17:20

标签: linux automation ansible devops

我想在剧本中将特定任务运行到单个主机。有没有更好的方法呢?

自定义剧本

---
- hosts: swarm_manager
  roles:
  - custom_role

角色/ custom_role /任务/ 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}')

1 个答案:

答案 0 :(得分:0)

您可以限制执行以下操作:

---
- 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}')

我已经用这种方式进行了测试:

---
- hosts: linux
  roles:
  - test_role

然后:

- 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}}"