Overview
Ansible is a powerful open-source automation tool that allows you to manage and configure servers, network devices, and applications. Although it is commonly used to automate tasks on remote hosts, it is also possible to run Ansible on your local machine without the need for a remote host. This is very useful if you are looking to quickly test some of the functionality locally.
In this blog post, we will discuss how to set up Ansible on your local machine and provide an example of how to append items to an existing list.
Setting Up Ansible on Your Local Machine
Before you can use Ansible on your local machine, you must first install it. The installation process varies depending on your operating system. You can find the installation instructions here - https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
Example
Once you have installed Ansible on your local machine, you need to create an inventory file to define the hosts and groups you want to manage. In our case, we can create an inventory file that only includes our local machine.
For this example, I'm creating three files which are shown below.
.
├── ansible.cfg
├── inventory
│ └── hosts.ini
└── playbook.yml
1 directory, 3 files
#ansible.cfg
[defaults]
inventory = /Users/suresh/Documents/ansible_local/inventory/hosts.ini
#hosts.ini
[localhost]
127.0.0.1
You can then finally create a playbook file that defines the tasks you want to run on your local machine. For example, you can create a playbook named playbook.yml
that appends items to an existing list.
This playbook defines a variable named my_list
that contains two items (item1
and item2
). It then uses the set_fact
module to append a third item (item3
) to the list. Finally, it uses the debug
module to display the contents of the my_list
variable.
#playbook.yml
---
- name: Appending items to an existing list
hosts: localhost
gather_facts: no
vars:
my_list:
- item1
- item2
tasks:
- name: Append item3 to list
set_fact:
my_list: "{{ my_list + ['item3'] }}"
- debug:
var: my_list
We can execute the playbook by running ansible-playbook playbook.yml
which will give the following output.
PLAY [Append items to list] **************************************************************************************************************************************************
TASK [Append item3 to list] **************************************************************************************************************************************************
ok: [127.0.0.1]
TASK [debug] *****************************************************************************************************************************************************************
ok: [127.0.0.1] => {
"my_list": [
"item1",
"item2",
"item3"
]
}
PLAY RECAP *******************************************************************************************************************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Closing Thoughts
In this blog post, we have discussed how to set up Ansible on your local machine and provided an example of how to append items to an existing list. Although Ansible is commonly used to automate tasks on remote hosts, it can also be a useful tool for automating tasks on your local machine.