Override hosts variable of Ansible playbook from the command line

前端 未结 11 1783
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 12:29

This is a fragment of a playbook that I\'m using (server.yml):

- name: Determine Remote User
  hosts: web
  gather_facts: false
  roles:
    - {         


        
11条回答
  •  情书的邮戳
    2020-12-12 13:21

    I am using ansible 2.5 (2.5.3 exactly), and it seems that the vars file is loaded before the hosts param is executed. So you can set the host in a vars.yml file and just write hosts: {{ host_var }} in your playbook

    For example, in my playbook.yml:

    ---
    - hosts: "{{ host_name }}"
      become: yes
      vars_files:
        - vars/project.yml
      tasks:
        ... 
    

    And inside vars/project.yml:

    ---
    
    # general
    host_name: your-fancy-host-name
    

提交回复
热议问题