« Ansible - Infrastructure-as-a-Code » : différence entre les versions

De Marijan Stajic | Wiki
Aller à la navigation Aller à la recherche
mAucun résumé des modifications
mAucun résumé des modifications
Ligne 7 : Ligne 7 :
Developing '''scripts''' for these tasks '''requires time''', '''coding skills''', and '''ongoing maintenance'''. With Ansible, automation becomes simple and efficient
Developing '''scripts''' for these tasks '''requires time''', '''coding skills''', and '''ongoing maintenance'''. With Ansible, automation becomes simple and efficient


== Configuration Files and Playbook ==
= Configuration Files =


Ansible provides a default '''configuration file''', usually located at '''/etc/ansible/ansible.cfg'''. This file '''contains various settings''' such as the inventory file location, SSH connection options, privilege escalation rules, and more. Depending on the project you're working on, '''you can define a separate configuration''' file in a '''different directory with custom settings'''. This is one way to override specific parameters in Ansible without changing the global config.
Ansible provides a default '''configuration file''', usually located at '''/etc/ansible/ansible.cfg'''. This file '''contains various settings''' such as the inventory file location, SSH connection options, privilege escalation rules, and more. Depending on the project you're working on, '''you can define a separate configuration''' file in a '''different directory with custom settings'''. This is one way to override specific parameters in Ansible without changing the global config.


In addition to the configuration file, you can create a '''playbook''', written in '''YAML'''. A playbook '''describes what tasks to run on which hosts'''. It typically defines target hosts, tasks to execute, roles to apply, variables, handlers, and other automation logic.
In addition to the configuration file, you can create a playbook, written in YAML. A playbook describes what tasks to run on which hosts. It typically defines target hosts, tasks to execute, roles to apply, variables, handlers, and other automation logic. Playbook is explain more in an another section.


Be careful, when '''running a playbook''', you should explicitly '''specify''' which '''configuration file to use''' by setting it through an environment variable, or directly in the Playbook file :
Be careful, when '''running a playbook''', you should explicitly '''specify''' which '''configuration file to use''' by setting it through an environment variable, or directly in the Playbook file :
Ligne 18 : Ligne 18 :
marijan$ ANSIBLE_CONFIG=/path/to/ansible.cfg ansible-playbook playbook.yml
marijan$ ANSIBLE_CONFIG=/path/to/ansible.cfg ansible-playbook playbook.yml
</pre>
</pre>
Here is the '''priority order Ansible''' uses to determine which configuration file to load :
# '''Environment variable''', you can specify the configuration file using the ANSIBLE_CONFIG environment variable, either when running the command or in your environment setup. <br>
# '''Configuration file in the current directory''', if there’s an ansible.cfg file in the same directory where the playbook is being run, Ansible will use it (as long as no environment variable overrides it). <br>
# '''Default configuration file''', if neither of the above is set, Ansible will fall back to the default configuration file located at /etc/ansible/ansible.cfg.
For example, if you have a playbook that currently relies on the default configuration file, but you '''want to change just one parameter''', you should first '''check the correct name of that parameter''' in the '''Ansible documentation''' or by using '''command'''. This is important because the name you see in the configuration file might not always match the name required for environment variables.
To see the '''full list of available parameters''' via the command line, run:
<pre class="linux"
marijan$ ansible-config list
</pre>
Once you've identified the correct variable name, you can use the '''export command (for a Linux shell)''' to set it as an '''environment variable''' :
<pre class="linux">
marijan$ export ANSIBLE_INVENTORY=/path/to/your/inventory
</pre>
This lets you '''override specific settings''' without modifying the entire config file. If you only want to change a parameter '''temporarily''', for a single run, you can also '''specify it directly in the command''' using the environment variable.
You can check the '''value of a specific parameter''' and see '''where it was defined''' (e.g., from an environment variable, a config file, etc.) by running the following command :
<pre class="linux">
marijan$ ansible-config dump | grep "parameter"
</pre>
Finally, to see '''which configuration file Ansible is currently using''', run :
<pre class="linux">
marijan$ ansible-config view"
</pre>
= Inventory =
= Variables =

Version du 14 avril 2025 à 20:43

Introduction

Ansible Banner.png

Ansible is an open-source automation and orchestration tool based on YAML and SSH, used to automate repetitive tasks in infrastructure management, such as provisioning, configuration management, continuous delivery, application deployment, and security compliance.

Developing scripts for these tasks requires time, coding skills, and ongoing maintenance. With Ansible, automation becomes simple and efficient

Configuration Files

Ansible provides a default configuration file, usually located at /etc/ansible/ansible.cfg. This file contains various settings such as the inventory file location, SSH connection options, privilege escalation rules, and more. Depending on the project you're working on, you can define a separate configuration file in a different directory with custom settings. This is one way to override specific parameters in Ansible without changing the global config.

In addition to the configuration file, you can create a playbook, written in YAML. A playbook describes what tasks to run on which hosts. It typically defines target hosts, tasks to execute, roles to apply, variables, handlers, and other automation logic. Playbook is explain more in an another section.

Be careful, when running a playbook, you should explicitly specify which configuration file to use by setting it through an environment variable, or directly in the Playbook file :

marijan$ ANSIBLE_CONFIG=/path/to/ansible.cfg ansible-playbook playbook.yml

Here is the priority order Ansible uses to determine which configuration file to load :

  1. Environment variable, you can specify the configuration file using the ANSIBLE_CONFIG environment variable, either when running the command or in your environment setup.
  2. Configuration file in the current directory, if there’s an ansible.cfg file in the same directory where the playbook is being run, Ansible will use it (as long as no environment variable overrides it).
  3. Default configuration file, if neither of the above is set, Ansible will fall back to the default configuration file located at /etc/ansible/ansible.cfg.

For example, if you have a playbook that currently relies on the default configuration file, but you want to change just one parameter, you should first check the correct name of that parameter in the Ansible documentation or by using command. This is important because the name you see in the configuration file might not always match the name required for environment variables.

To see the full list of available parameters via the command line, run:


Once you've identified the correct variable name, you can use the '''export command (for a Linux shell)''' to set it as an '''environment variable''' :
<pre class="linux">
marijan$ export ANSIBLE_INVENTORY=/path/to/your/inventory

This lets you override specific settings without modifying the entire config file. If you only want to change a parameter temporarily, for a single run, you can also specify it directly in the command using the environment variable.

You can check the value of a specific parameter and see where it was defined (e.g., from an environment variable, a config file, etc.) by running the following command :

marijan$ ansible-config dump | grep "parameter"

Finally, to see which configuration file Ansible is currently using, run :

marijan$ ansible-config view"

Inventory

Variables