ansible inventory

Core Idea

localhost is already in the inventory: Ansible runs tasks on the control machine over the local connection plugin, bypassing SSH, so you never add it by hand.

  • localhost is included automatically and uses the local connection by default.
  • Efficient for local operations without inventory entries.
  • A worked example shows tasks targeting the control machine.

In Ansible localhost is automatically included in the inventory by default.
This means you can run tasks on the control machine (the machine where Ansible is executed) without needing to explicitly define it in the inventory file.

  • By default, Ansible uses the local connection plugin for localhost, bypassing SSH. This makes it efficient for local operations.
  • Example configuration (inferred automatically):
[localhost] 
localhost ansible_connection=local

Example

- name: Create directory and copy files on localhost
  hosts: localhost
  become: false  # No privilege escalation needed unless required
  tasks:
    - name: Create a directory
      ansible.builtin.file:
        path: /tmp/example_directory
        state: directory
        mode: '0755'
 
    - name: Copy a file to the directory
      ansible.builtin.copy:
        src: /path/to/local_file.txt
        dest: /tmp/example_directory/local_file.txt
        mode: '0644'