0%

0/1 Lessons

Course Introduction

• 19min

0 / 2 lessons complete

IT Lab Setup

• 31min

0 / 3 lessons complete

Installing Ansible

• 57min

0 / 4 lessons complete

Managing your Ansible Inventory

• 2hr 45min

0 / 8 lessons complete

Ansible Basics

• 2hr 55min

0 / 9 lessons complete

Ansible Roles

• 2hr 40min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 42min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 23min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 39min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Creating an Ansible Config File

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, you will learn about Ansible Config files and how to create them. It's important for you to know how to create and work with config files as they can save you from having to repeat certain arguments every time you run Ansible. Let's get started!

What is an Ansible Config File?

An Ansible config file tells Ansible about your environment. The ansible-config utility that comes with Ansible allows you to see all the configuration settings you can set inside of this config file. Ansible will search for config files in the following order:

We're going to use the ansible.cfg in our current directory to define the default inventory file that should be used when running Ansible in that directory.

Configuring our Custom Inventory as Default

When we ran the ping command earlier, we had to specify the inventory file we want to use with the -i argument like so:

ansible webservers -i inventory -m ping

This is tedious and since you would need to do this every time you ran Ansible, and if we fail to use the -i argument, we see a message like that below, and worst of all, no action is taken by our Ansible controller:

image 19
Ansible - No Inventory Found

One way to avoid this issue is by creating an Ansible config file. As we stated before, if there is a file named ansible.cfg in the current directory where you are running your Ansible commands, it will be used as the default config.

Inside that config file, in INI format, we can specify the default inventory setting. We can either create a blank config file with the touch command, or use the ansible-config command to create a fully commented out config file, or do both which is what we will do.

Create a fully commented out example Config

If you'd prefer to have a look at all the options you could configure, you can run this command to create an example config file:

ansible-config init --disabled > ~/code/ansible.cfg.example

Notice that I named the second file ansible.cfg.example, this is because it contains a lot of settings that we simply don't need and I only want the file there for reference. Notice the extension is .example, so Ansible will not use this file as its config. It is purely there for reference.

Create a blank default config

Now below, let's create our real Ansible Config:

touch ~/code/ansible.cfg

Open the blank ansible.cfg file with nano:

nano ~/code/ansible.cfg

Add the following code to our config file:

[defaults]
# Specify our default inventory file
inventory = ~/inventory

Save and close the file. Now we can run our Ansible ping command without issue:

ansible webservers -m ping

And we should get an output like so:

image 20
Pinging Ansible Inventory without needing Custom Inventory File Argument

Conclusion

That wraps up this lesson on creating Ansible Config files. If you want to learn more about the Ansible config options you have, refer to the ansible-config utility. See you in the next lesson!

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments