DevOps

Using Ansible Command Line

Ansible is a powerful automation tool widely used for configuration management, application deployment, and task automation. It can manage systems remotely or locally. Here’s how you can use Ansible to run playbooks locally.

Using the Ansible Command Line

To execute a playbook on the local machine using the command line, use the following command:

ansible-playbook --connection=local 127.0.0.1 playbook.yml

Explanation:

  • ansible-playbook: This is the command used to run Ansible playbooks.
  • --connection=local: This flag tells Ansible to run the playbook locally.
  • 127.0.0.1: This is the IP address for the localhost.
  • playbook.yml: This is the playbook file that contains the automation tasks.

Example:

If you have a playbook named install_apache.yml to install Apache web server, you would run:

ansible-playbook --connection=local 127.0.0.1 install_apache.yml

Using Inventory

Ansible uses inventory files to define the hosts on which commands should be executed. To specify that a playbook should run locally using an inventory file, you can add the following line:

127.0.0.1 ansible_connection=local

Explanation:

  • 127.0.0.1: The IP address for the localhost.
  • ansible_connection=local: This parameter tells Ansible to use a local connection.

Example:

Create an inventory file hosts.ini with the content:

127.0.0.1 ansible_connection=local

Then run the playbook using this inventory file:

ansible-playbook -i hosts.ini playbook.yml

Using Ansible Configuration File

You can set the connection type in the Ansible configuration file to run all playbooks locally by default. Add the following lines to the ansible.cfg file:

[defaults]
transport = local

Explanation:

  • [defaults]: This section defines default settings for Ansible.
  • transport = local: This sets the default connection type to local.

Example:

Add the above configuration to ansible.cfg:

[defaults]
transport = local

Then run any playbook without specifying the connection type:

ansible-playbook playbook.yml

Using Playbook Header

Another way to specify the connection type directly within the playbook is by setting it in the playbook header:

- hosts: 127.0.0.1
  connection: local

Explanation:

  • hosts: 127.0.0.1: Specifies that the playbook should run on the localhost.
  • connection: local: Defines the connection type as local.

Example:

A playbook install_apache.yml might start with:

- hosts: 127.0.0.1
  connection: local

  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present

Then run the playbook as usual:

ansible-playbook install_apache.yml
Ali Imran
Over the past 20+ years, I have been working as a software engineer, architect, and programmer, creating, designing, and programming various applications. My main focus has always been to achieve business goals and transform business ideas into digital reality. I have successfully solved numerous business problems and increased productivity for small businesses as well as enterprise corporations through the solutions that I created. My strong technical background and ability to work effectively in team environments make me a valuable asset to any organization.
https://ITsAli.com

Leave a Reply