首页 > 解决方案 > 我可以在 Ansible 任务中的特定主机或主机组上运行吗?

问题描述

我可以在 Ansible 任务中的特定主机或主机组上运行吗?

---
- hosts: all
  become: yes

  tasks:
    - name: Disable tuned
      hosts: client1.local
      service:
        name: tuned
        enabled: false
        state: stopped

无论如何它都不起作用。这是错误:

[root@centos7 ansible]# ansible-playbook playbook/demo.yaml
ERROR! conflicting action statements: hosts, service

The error appears to be in '/root/ansible/playbook/demo.yaml': line 24, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Disable tuned
  ^ here

标签: ansible

解决方案


由于我对将主机组织成组有类似的要求,因此我发现以下方法对我有用。

首先,我根据我的环境、管理组和任务来构建我的库存。

存货

[infrastructure:children]
prod
qa
dev

[prod:children]
tuned_hosts

[tuned_hosts]
client1.local

然后我可以使用

剧本

---
- hosts: all
  become: yes

  tasks:
    - name: Disable tuned
      service:
        name: tuned
        enabled: false
        state: stopped
      when: ('tuned_hosts' in group_names) # or ('prod' in group_names)

以及类似的东西

  when: ("dev" not in group_names)

取决于我试图实现的目标。

文档


推荐阅读