Override hosts variable of Ansible playbook from the command line

前端 未结 11 1767
隐瞒了意图╮
隐瞒了意图╮ 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:33

    I'm using another approach that doesn't need any inventory and works with this simple command:

    ansible-playbook site.yml -e working_host=myhost
    

    To perform that, you need a playbook with two plays:

    • first play runs on localhost and add a host (from given variable) in a known group in inmemory inventory
    • second play runs on this known group

    A working example (copy it and runs it with previous command):

    - hosts: localhost
      connection: local
      tasks:
      - add_host:
          name: "{{ working_host }}"
          groups: working_group
        changed_when: false
    
    - hosts: working_group
      gather_facts: false
      tasks:
      - debug:
          msg: "I'm on {{ ansible_host }}"
    

    I'm using ansible 2.4.3 and 2.3.3

提交回复
热议问题