5

I'm trying to simply connect to a machine and run the win_ping module.

There are several tutorials online that outline a few key steps:

  • Create a directory ./windows
  • Create a file hosts in ./windows
  • Create a directory ./windows/group_vars
  • Create a file windows.yml in ./windows/group_vars
  • Insert several variables with your Windows credentials into windows.yml

The problem is that none of these actually specify where to put these relative to the '.'. I've tried placing this structure in /etc/ansible/ and unfortunately it's looking like Ansible isn't picking this up at all. I've also tried moving the structure around but no luck.

How do I get Ansible to reference the directory I've created? I believe ansible.cfg would make sense but I can only see how to point it to a default inventory file. Interestingly enough - even adding a group called 'windows' in the base hosts file and calling ansible with the group there - it says no hosts found. I have also uncommented the inventory location in ansible.cfg to point to /etc/ansible/hosts and it still says no hosts found when calling ansible windows -m ping (or win_ping for that matter).

5

There are two places where Ansible looks for group_vars subdirectory:

  • the playbook directory - the one containing a playbook you run
  • the inventory directory - the one specified with -i option at run time, or a default one (usually /etc/ansible, or /usr/local/etc/ansible)

In most typical scenario you might place group_vars along with the playbook.yml.

The example you posted uses the second method, so to reference the files you need to add -i to the command:

ansible windows -m ping -i ./windows
0

There are a few GitHub repos regarding Ansible best practices, and I've found enginyoyen/ansible-best-practices helpful not only in your case but also to understand the playbook/inventory/role/task differences in a full-blown Ansible project.

Some highlights: (/ refers to the project's home directory)

  • Your variables do indeed go in /group_vars/
    • /group_vars/windows.yml will be automatically read by hosts you assign to the windows group
    • /group_vars/db.yml will be automatically read by hosts you assign to the db group and so on
  • You may also include files with names like /host_vars/win_host_1.yml which will be read only by that host
  • The groups from /group_vars/ are defined in your inventory files (plural)

    [windows]
    win_host1.example.com
    win_host_2.example.com
    
  • Inventory files have names like /production.yml and /staging.yml to help ensure code in different states of readiness is deployed to (ideally) identical environments.

Other highlights not directly answering your question

  • A role is a file made of the tasks required to do something, like install nginx. They have names like /roles/install_nginx.yml
  • Playbooks have names like /playbooks/win_hosts.yml which do nothing but match the groups from your inventory files with the roles you want hosts in those groups to run.
  • When you run ansible-playbook your $HOME should be /playbooks/ so that the different inventory files are referred as ansible-playbook -i ../production.yml win_hosts.yml
  • This is also the place to put a custom ansible config file, /playbooks/ansible.cfg
  • There's also a way to manage external roles from Ansible Galaxy or GitHub and keep them separate from custom roles you built, but still in your project directory instead of /etc/ansible/roles/ where ansible-galaxy puts them by default.
  • A nice little bash script to install Ansible

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.