how to run a particular task on specific host in ansible

前端 未结 3 979
遥遥无期
遥遥无期 2021-01-07 16:40

my inventory file\'s contents -

[webservers]
x.x.x.x ansible_ssh_user=ubuntu

[dbservers]
x.x.x.x ansible_ssh_user=ubuntu

in my tasks file

3条回答
  •  抹茶落季
    2021-01-07 17:36

    An alternative to consider in some scenarios is -

    delegate_to: hostname
    

    There is also this example form the ansible docs, to loop over a group. 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']}}"
    

提交回复
热议问题