Skip to content

Instantly share code, notes, and snippets.

@0xSheepdog
Last active January 20, 2021 00:15
Show Gist options
  • Save 0xSheepdog/4d6cd532d3310210a1ffc32401853a3f to your computer and use it in GitHub Desktop.
Save 0xSheepdog/4d6cd532d3310210a1ffc32401853a3f to your computer and use it in GitHub Desktop.
A simple, quick ansible.cfg example file. AND a not quite so simple YAML formatted inventory/hosts file. Examples for multiple group membership, host and group variables ... please review the official doco.
[defaults]
inventory = ./inventory.yml
vault_password_file = .vaultpass
retry_files_enabled = false
roles_path = ./roles
gathering = smart
callback_whitelist = timer, profile_tasks
[privilege_escalation]
become = true
become_method = sudo
become_user = root
all:
hosts:
jump-server.example.com:
ansible_port: 5555
ansible_host: 192.0.2.50
children:
webservers:
hosts:
http-foo.example.com:
http2-foo.example.com:
nginx[1:4].example.com:
dbservers:
hosts:
mariasql.example.com:
pgsql1.example.com:
pgsql2.example.com:
mysql[1:5].example.com:
production:
hosts:
http-foo.example.com:
nginx1.example.com:
nginx2.example.com:
pgsql1.example.com:
mysql[1:2].example.com:
vars:
remote_user: productionpush
test:
hosts:
http2-foo.example.com:
nginx[3:4].example.com:
pgsql2.example.com:
mysql[3:4].example.com:
@0xSheepdog
Copy link
Author

0xSheepdog commented Jan 20, 2021

Please review the inventory.yml carefully if you are using this. Ref: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#host-variables

Output from my ansible-inventory --list -i inventory.yml

$ ansible-inventory --list -i inventory.yml
{
    "_meta": {
        "hostvars": {
            "http-foo.example.com": {
                "remote_user": "productionpush"
            },
            "jump-server.example.com": {
                "ansible_host": "192.0.2.50",
                "ansible_port": 5555
            },
            "mysql1.example.com": {
                "remote_user": "productionpush"
            },
            "mysql2.example.com": {
                "remote_user": "productionpush"
            },
            "nginx1.example.com": {
                "remote_user": "productionpush"
            },
            "nginx2.example.com": {
                "remote_user": "productionpush"
            },
            "pgsql1.example.com": {
                "remote_user": "productionpush"
            }
        }
    },
    "all": {
        "children": [
            "dbservers",
            "production",
            "test",
            "ungrouped",
            "webservers"
        ]
    },
    "dbservers": {
        "hosts": [
            "mariasql.example.com",
            "mysql1.example.com",
            "mysql2.example.com",
            "mysql3.example.com",
            "mysql4.example.com",
            "mysql5.example.com",
            "pgsql1.example.com",
            "pgsql2.example.com"
        ]
    },
    "production": {
        "hosts": [
            "http-foo.example.com",
            "mysql1.example.com",
            "mysql2.example.com",
            "nginx1.example.com",
            "nginx2.example.com",
            "pgsql1.example.com"
        ]
    },
    "test": {
        "hosts": [
            "http2-foo.example.com",
            "mysql3.example.com",
            "mysql4.example.com",
            "nginx3.example.com",
            "nginx4.example.com",
            "pgsql2.example.com"
        ]
    },
    "ungrouped": {
        "hosts": [
            "jump-server.example.com"
        ]
    },
    "webservers": {
        "hosts": [
            "http-foo.example.com",
            "http2-foo.example.com",
            "nginx1.example.com",
            "nginx2.example.com",
            "nginx3.example.com",
            "nginx4.example.com"
        ]
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment