deploy.yaml
---
- name: test
  hosts: host_a
  tasks:
    - name: debug
      debug:
        var: demo
inventory
[host_a]
localhost
[host_a:vars]
demo=aaa
[host_b]
localhost
[host_b:vars]
demo=bbb
When I run the playbook, I want the variable demo to return aaa, but it returns bbb.
Why?
                        
Because a variable defined in inventory is treated as a fact, and a fact is bound to a host in Ansible. As you define only one host, named
localhost, the first value gets overwritten.Confirm with:
or