Search code examples
ansibleansible-inventory

ansible vagrant windows and localhost


I've got a vagrant box that I use win_rm for, however, I need to fetch a file from it and use blockinfile on my localhost (MacOs) and then copy the file back to the vagrant box.

Ansible does not like having two 127.0.0.1 items in the inventory. I've tried just about everything I can think of can't get them to work together.

The vagrant running on VirtualBox has NAT setup but I can't seem to access it other than through the loopback address. That might solve my issue.

I've also tried setting different IPs in the Vagrantfile and those have not been fruitful either.

Below is the inventory file that I've been working with.

[win]
127.0.0.1


[localhost]
control_machine ansible_host=local

[win:vars]
ansible_port=55985
ansible_winrm_transport=basic
ansible_winrm_scheme=http
ansible_user=vagrant
ansible_password="{{ lookup('env', 'WIN_GUEST_PASSWORD') }}"
nsible_connection=winrm


[localhost:vars]
ansible_user=test
ansible_connection=local
ansible_python_interpreter="/Library/Frameworks/Python.framework/Versions/3.8/bin/python3"

Solution

  • There are a few things to say about your inventory:

    1. You seem to be confusing the concept of group and host
    2. You are defining a group that has the same name as an implicit host (i.e. localhost) (maybe because of point 1)
    3. You are explicitly defining a host for your ansible controller when this is actually probably not what you want since ansible defines an implicit localhost for you. Note that an explicit definition makes your controller match the all magic group which is typically not wanted in most situations.

    From the information you gave, here is how I would write my inventory. The examples below use the ansible capability to organize vars in seperate files/folders. Please have a look at the inventory documentation for more info. I also used the yaml format for the inventory as it is easier to understand IMO. Feel free to translate back to ini if you whish

    in inventories/my_env/hosts.yml (any filename will do)

    ---
    all:
      hosts:
        my.windows.vagrant:
    

    in inventories/my_env/host_vars/my.windows.vagrant.yml

    ---
    ansible_host: 127.0.0.1
    ansible_port: 55985
    ansible_winrm_transport: basic
    ansible_winrm_scheme: http
    ansible_user: vagrant
    ansible_password: "{{ lookup('env', 'WIN_GUEST_PASSWORD') }}"
    ansible_connection: winrm
    

    in inventories/my_env/host_vars/localhost.yml

    ---
    ansible_python_interpreter: "/Library/Frameworks/Python.framework/Versions/3.8/bin/python3"
    

    Note that I am not (re)defining the implicit localhost in the inventory, only defining the (non standard) python interpreter you use on that host. Note as well that I dropped the other vars for localhost as:

    1. implicit localhost uses the local connection plugin by default
    2. the local connection plugin does not take ansible_user into account and use the already logged in user (i.e. the one lauching the playbook on the controller)

    Once you have done this, you can use the my.windows.vagrant target to address your vagrant windows box and localhost to run things on the controller.

    Adapt to your exact needs.