Ansible hello world

You want to start with ansible? Good luck. Bye… Oh no! I’m writing a blog. I must help you. Let’s start.
Install Ansible
I assume you have python3 / pip / virtualenv working on your system. We don’t want to mess up, so we’re going to use virtualenv.
virtualenv venv
. venv/bin/activate
pip install ansible
That’s it. You can check it out typing ansible --version.
Config file.
Create a config file. ansible.cfg in your working directory.
[defaults]
inventory = testinv.yaml
interpreter_python = auto_silent
nocows = true
Let me explain.
[defaults] - most of interesting settings goes to this section.
inventory - our inventory. We’ll create it in the next step.
interpreter_python - suppresses warning about python interpreter.
nocows - suppresses using cowsay for displaying messages. Use at your own risk. ;)
Inventory
Let’s create testinv.yaml file. You can also use ini format. I prefer yaml. Up to you.
---
all:
children:
testgroup:
hosts:
testhost:
ansible_connection: local
all - the root group name. Everything goes there.
children - a group can have subgroups. List them under children
testgoup - our actual group name
hosts - of course group can contain hosts…
testhosts - a host. A kind of fake hosts for testing purpose. Normally the names must be resolvable in DNS. Or in /etc/hosts. Or be IP addresses.
ansible_connaction - Describes way of connecting the host. local means localhost. Everything will be executed locally. It means the name from previous line doesn’t really matter.
The first playbook
Let’s create the file - hello.yaml'. Yes. While using ansible - you’re and yaml developer.
---
- hosts: testgroup
tasks:
- debug:
msg: Hello World!
Quite obvious. The playbook is executed on all boxes listed in the testgoup group. We’re going to display “Hello World!” message.
Run it
The command is simple:
ansible-playbook hello.yaml
If you’re lucky and did not make any silly mistake - you’ll see:

That’s it. I hereby declare you and automation guru. Congratulations.




