Creating an Ansible Config File
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
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.
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:
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:
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.