How do i use static and dynamic inventories(aws) without the ansible inventory option?



  • Dynamic inventories are usually provided by the vendor, aws, openstack, etc

    Static inventories you create a list of hosts in a static file, usually named "hosts".

    The issue is you normally want a way of just using groups of hosts to apply a configuration to using ansible, without specifying the default inventory name using the -i option.

    In this article the ansible_env will mean you location of your ansible directory structure, for examples on directory structure refer to this page: http://docs.ansible.com/ansible/latest/playbooks_best_practices.html

    On that page ansible gave an example using a directory named inventories, here we are going to call ours environments.

    $ cd /ansible_env/environments/test
    $ tree
    .
    ├── ec2.ini -> ../../contrib/inventory/ec2.ini
    ├── ec2.py -> ../../contrib/inventory/ec2.py
    ├── group_vars
    │   ├── group1
    │   └── group2
    ├── hosts
    └── host_vars
        ├── testhost1
        └── testhost2
    

    For the dynamic inventory we link back to contrib, this way we only update one set of files when updating the script versions.

    For the static inventory we just use hosts.

    Similar environments can be setup for prod, stage, dev, etc.

    Alternatively you if do not want to separate out your environments like this, have the structure directory in the environments directory.

    $ cd cd /ansible_env/environments
    $ tree .
    .
    ├── ec2.ini -> ../contrib/inventory/ec2.ini
    ├── ec2.py -> ../contrib/inventory/ec2.py
    ├── group_vars
    │   ├── group1
    │   └── group2
    ├── hosts
    └── host_vars
        ├── testhost1
        └── testhost2
    

    This allows you to run commands without the -i option for inventory.

    $ ansible mywebservers -m shell -a "uptime"
    

Log in to reply
 

© Lightnetics 2024