Ansible Quick Test

When I was working at IBM, I applied a dedicated cluster for Ansible learning. After I left, I decide to use Vagrant to create local cluster for the same purpose.

NOTE: I have also created a docker sponsored Ansible testing environment, please see here

Please check Vagrant Ansible testing cluster repo. Follow the README to set up and play with ansible. The problems I had at the time of creating the repo:

  1. how to establish the SSH connection to Vagrant VM.
  2. the sed insert has subtle difference in Mac.

Ansible Install

Ansible Install guide

  • Control node requirements: Starting with ansible-core 2.11, the project will only be packaged for Python 3.8 and newer.

If you are using Ansible to manage machines in a cloud, consider using a machine inside that cloud as your control node. In most cases Ansible will perform better from a machine on the cloud than from a machine on the open Internet.

Managed node requirements: Although you do not need a daemon on your managed nodes, you do need a way for Ansible to communicate with them. For most managed nodes, Ansible makes a connection over SSH and transfers modules using SFTP. For any machine or device that can run Python, you also need Python 2 (version 2.6 or later) or Python 3 (version 3.5 or later).

If install on Linux using yum (I use pip install in virtualenv in the demo, see repo README):

1
2
3
4
5
6
7
8
9
# search ansible package
# ansible.noarch
# ansible-python3.noarch
# SSH-based configuration management, deployment, and task execution system
yum search ansible
# python2
sudo yum install -y -q ansible
# python3
sudo yum install -y -q ansible-python3

Ansible Inventory

How to build your inventory, for inventory file, 主要涉及一些ssh connection的设置. Position the target hosts from group

Ansible Config

Ansible Configuration Settings for ansible.cfg file.

Ansible Yaml Format

Yaml syntax, Especially the difference between > and | for multi-line coding:

Spanning multiple lines using a | will include the newlines and any trailing spaces. Using a > will fold newlines to spaces; In either case the indentation will be ignored.

1
2
3
4
5
6
7
8
9
include_newlines: |
exactly as you see
will appear these three
lines of poetry

fold_newlines: >
this is really a
single line of text
despite appearances

Ansible Run

Ad-hoc command example:

1
2
3
4
5
# -v: verbose, display output
# can specify single machine
ansible -v -i vagrant_ansible_inventory.ini worker1 -m ping
# all
ansible -v -i vagrant_ansible_inventory.ini all -m shell -a 'echo $(whoami)'

Playbook Role, check role directory structure and how to use role.

1
2
3
4
5
6
7
8
9
# -e|--extra-vars: pass extra variables
# -b: become
# -v: verbose
ansible-playbook [-b] -v -i vagrant_ansible_inventory.ini setup.yml \
-e '{"version":"1.10.5","other_variable":"foo"}' # json format

ansible-playbook [-b] -v -i vagrant_ansible_inventory.ini setup.yml \
-e "foo=23" \
-e "bar=hello"
0%