Trying it out
Overview
Teaching: 25 min
Exercises: 0 minQuestions
Objectives
Try our some basic Ansible code
This episode describes the tools we use to build and manage lessons. These simplify many tasks, but make other things more complicated.
Trying it out
Let’s create a folder to get started.
$ mkdir ~/ansible-training && cd ~/ansible-training
1. Module: Ping
$ ansible localhost -m ping
[WARNING]: Host file not found: /usr/local/etc/ansible/hosts
[WARNING]: provided hosts list is empty, only localhost is available
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
2. Module: Setup
$ ansible localhost -m setup
..... woah!
Trying again: what info are we interest in? e.g. CPU cores:
$ ansible localhost -m setup | grep processor_cores
[WARNING]: Host file not found: /usr/local/etc/ansible/hosts
[WARNING]: provided hosts list is empty, only localhost is available
"ansible_processor_cores": "4",
What’s with all these [WARNING] statements?!
Ansible is looking for an inventory file - let’s create one.
3. Inventory Files
Here’s we’ll create an inventory file for eel and cowfish
$ echo eel.openmicroscopy.org >> inventory-file
$ echo cowfish.openmicroscopy.org >> inventory-file
$ cat inventory-file
eel.openmicroscopy.org
cowfish.openmicroscopy.org
Rather than typing yes
for every host, ssh-keyscan
takes care of that.
$ cat inventory-file | grep -vP '[\[#]' | grep -P '\.' | sort | uniq | while read h; do ssh-keyscan $h >> ~/.ssh/known_hosts; done
Trying your first ad-hoc command:
$ ansible all -i inventory-file -a "/bin/echo hello"
A real-world example: nginx versions
$ ansible all -i inventory-file -m shell -a 'rpm -q nginx'
Further reading at ansible.com - getting started
Key Points
Run simple shell commands
Add more hosts inventory files
Gather data from multiple hosts in one command