In Ansible, how do I assign a hostvar to a playbook's host?



  • The directory of my Ansible project has the following structure:

    project/
      playbook.yml
      host_vars/
        localhsot.yml
      roles/
        ...
        terraform-provision/
          ...
      terraform/
          ...
    

    Here's project/playbook.yml:

    - hosts: localhost
      tasks:
      - name: trigger role
        include_role:
          name: terraform-provision
    - hosts: hostvars['localhost'].aws_instance_host_group
      tasks:
      - name: debugging
        debug:
          var: hostvars['localhost'].aws_instance_host_group
    

    Here's the relevant part of project/host_vars/localhost.yml:

    hostvars['localhost'].aws_instance_host_group
    aws_instance_host_group: "aws_instance"
    

    Here's the relevant part of project/roles/terraform-provision/tasks/main.yml:

    - name: Add the provisioned AWS instance to the inventory
      add_host:
        name: "{{ aws_instance_public_ip.value }}" # set using set_fact elsewhere in file
        ansible_user: "{{ aws_instance_user_name }}"
        groups: "{{ aws_instance_host_group }}"
    

    When I execute project/playbook.yml, it throws the following error when trying to connect to the host created by the terraform-provision role:

    [WARNING]: Could not match supplied host pattern, ignoring: hostvars['localhost'].aws_instance_host_group
    

    However, the debugging task of project/playbook.yml prints the group's name (aws_instance) successfully.

    What am I missing? Is there a better approach to achieve the same thing?



  • To be evaluated, a variable must be closed in https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#when-to-quote-variables-a-yaml-gotcha , e.g.

    - hosts: "{{ hostvars.localhost.aws_instance_host_group }}"
    

    The https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#referencing-key-value-dictionary-variables simplifies the code.


    https://stackoverflow.com/help/minimal-reproducible-example

    shell> cat pb.yml
    ---
    - hosts: localhost
      gather_facts: false
      tasks:
        - set_fact:
            aws_instance_host_group: aws_instance
        - add_host:
            name: 10.1.0.61
            groups: "{{ aws_instance_host_group }}"
    
    • hosts: "{{ hostvars.localhost.aws_instance_host_group }}"
      gather_facts: false
      tasks:
      • debug:
        var: groups
      • debug:
        var: inventory_hostname
    shell> cat hosts
    localhost
    
    shell> ansible-playbook -i hosts pb.yml 
    
    PLAY [localhost] *****************************************************************************
    
    TASK [set_fact] ******************************************************************************
    ok: [localhost]
    
    TASK [add_host] ******************************************************************************
    changed: [localhost]
    
    PLAY [aws_instance] **************************************************************************
    
    TASK [debug] *********************************************************************************
    ok: [10.1.0.61] => 
      groups:
        all:
        - 10.1.0.61
        - localhost
        aws_instance:
        - 10.1.0.61
        ungrouped:
        - localhost
    
    TASK [debug] *********************************************************************************
    ok: [10.1.0.61] => 
      inventory_hostname: 10.1.0.61
    
    PLAY RECAP ***********************************************************************************
    10.1.0.61: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    localhost: ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2