如何 运行 ansible 中特定主机上的特定任务
how to run a particular task on specific host in ansible
我的库存文件的内容 -
[webservers]
x.x.x.x ansible_ssh_user=ubuntu
[dbservers]
x.x.x.x ansible_ssh_user=ubuntu
在我的任务文件中,它具有共同的作用,即它将在两台主机上 运行 但我想 运行 在主机网络服务器上执行以下任务,而不是在库存文件中定义的数据库服务器中 运行 =13=]
- name: Install required packages
apt: name={{ item }} state=present
with_items:
- '{{ programs }}'
become: yes
tags: programs
是什么时候模块有帮助还是有其他方法?我怎么能这样做?
如果你想 运行 你在所有主机上的角色,但只有一个任务仅限于 webservers
组,那么 - 正如你已经建议的那样 - when
是你的朋友。
您可以定义如下条件:
when: inventory_hostname in groups['webservers']
谢谢,这对我也有帮助。
主机文件:
[production]
host1.dns.name
[internal]
host2.dns.name
requirements.yml 文件:
- name: install the sphinx-search rpm from a remote repo on x86_64 - internal host
when: inventory_hostname in groups['internal']
yum:
name: http://sphinxsearch.com/files/sphinx-2.2.11-1.rhel7.x86_64.rpm
state: present
- name: install the sphinx-search rpm from a remote repo on i386 - Production
when: inventory_hostname in groups['production']
yum:
name: http://sphinxsearch.com/files/sphinx-2.2.11-2.rhel6.i386.rpm
state: present
在某些情况下要考虑的替代方案是 -
delegate_to: hostname
ansible 文档中也有这个示例,用于遍历一个组。 https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html -
- hosts: app_servers
tasks:
- name: gather facts from db servers
setup:
delegate_to: "{{item}}"
delegate_facts: True
loop: "{{groups['dbservers']}}"
我的库存文件的内容 -
[webservers]
x.x.x.x ansible_ssh_user=ubuntu
[dbservers]
x.x.x.x ansible_ssh_user=ubuntu
在我的任务文件中,它具有共同的作用,即它将在两台主机上 运行 但我想 运行 在主机网络服务器上执行以下任务,而不是在库存文件中定义的数据库服务器中 运行 =13=]
- name: Install required packages
apt: name={{ item }} state=present
with_items:
- '{{ programs }}'
become: yes
tags: programs
是什么时候模块有帮助还是有其他方法?我怎么能这样做?
如果你想 运行 你在所有主机上的角色,但只有一个任务仅限于 webservers
组,那么 - 正如你已经建议的那样 - when
是你的朋友。
您可以定义如下条件:
when: inventory_hostname in groups['webservers']
谢谢,这对我也有帮助。
主机文件:
[production]
host1.dns.name
[internal]
host2.dns.name
requirements.yml 文件:
- name: install the sphinx-search rpm from a remote repo on x86_64 - internal host
when: inventory_hostname in groups['internal']
yum:
name: http://sphinxsearch.com/files/sphinx-2.2.11-1.rhel7.x86_64.rpm
state: present
- name: install the sphinx-search rpm from a remote repo on i386 - Production
when: inventory_hostname in groups['production']
yum:
name: http://sphinxsearch.com/files/sphinx-2.2.11-2.rhel6.i386.rpm
state: present
在某些情况下要考虑的替代方案是 -
delegate_to: hostname
ansible 文档中也有这个示例,用于遍历一个组。 https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html -
- hosts: app_servers
tasks:
- name: gather facts from db servers
setup:
delegate_to: "{{item}}"
delegate_facts: True
loop: "{{groups['dbservers']}}"