0%

Ansible part 1: deploy enviroment and most important concepts

what is Ansible

Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.

There are three main parts in Ansible: inventory, playbook and modules.

Inventory

Inventory is a list of managed nodes. An inventory file is also sometimes called a “hostfile”. Your inventory can specify information like IP address for each managed node. An inventory can also organize managed nodes, creating and nesting groups for easier scaling.

For my enviroment, there are two nodes:

1
2
3
4
[web1]
ansible-node1 ansible_connection=ssh ansible_user=pi
[web2]
cube4200 ansible_connection=ssh ansible_user=anna

Playbooks

Playbooks are Ansible’s configuration, deployment and orchestration language. Each playbook is composed of one or more ‘plays’ in a list.

Playsbook is yaml file. there are listed tasks you want ansible to work.
Playbook is the most important file in ansible.

In playbooks, there are some important concepts, such as variables, variables files, loops.

variables

playbook variables can be wrote directly in playbook or in variable files. all kinds variabls have their precendence during playbook exectued.

Loops

with_items is used in single loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- name: hello loop
hosts: localhost
gather_facts: no
vars:
test:
- test1
- test2
- test3
- test4
tasks:
- name: Test loop
debug:
msg: "{{ item }}"
with_items: "{{ test }}"

after runing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
anna@ansible-controller:~/ansible-code/inventory/loop$ ansible-playbook helloloop.yml 

TASK [Test loop] *******************************************************************************************************
ok: [localhost] => (item=None) => {
"msg": "test1"
}
ok: [localhost] => (item=None) => {
"msg": "test2"
}
ok: [localhost] => (item=None) => {
"msg": "test3"
}
ok: [localhost] => (item=None) => {
"msg": "test4"
}

PLAY RECAP *************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0

with_nested is used in multiple loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- name: nested loop
hosts: localhost
gather_facts: no
vars:
test:
- test1
- test2
- test3
- test4
demo:
- demo1
- demo2
- demo3
xyz:
- xyz1
- xyz2
tasks:
- name: Test loop
debug:
msg: "{{ item[0] }}, {{ item[1] }}, {{ item[2] }}"
with_nested:
- "{{ test }}"
- "{{ demo }}"
- "{{ xyz }}"

after runing, for loop three iteration and it will show 24 pieces of message.

Notice: yml file is very strict with indention. BE CARE!

condition in playbook

when is the key word in playbook if you want to put some condition in it.

for example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- name: test conditon
hosts: localhost
gather_facts: no
vars:
seq:
- 1
- 2
- 3
- 4
tasks:
- name: test loop and when
debug:
msg: "{{ item }}"
with_items: "{{ seq }}"
when: item >=3

after running,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
anna@ansible-controller:~/ansible-code/inventory/condition$ ansible-playbook when.yml 
PLAY [test conditon] ***************************************************************************************************
TASK [test loop and when] **********************************************************************************************
skipping: [localhost] => (item=None)
skipping: [localhost] => (item=None)
ok: [localhost] => (item=None) => {
"msg": 3
}
ok: [localhost] => (item=None) => {
"msg": 4
}

PLAY RECAP *************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0

Only item which is greater than 2 can be exectued, so seq 1 and seq2 will be skipped.

Notice: “and” and “or” in condition, look up the documents ansible conditon syntax

Modules

ansible modules are units of code Ansible executes. Each module has a particular use, from administering users on a specific type of database to managing VLAN interfaces on a specific type of network device. You can invoke a single module with a task, or invoke several different modules in a playbook.Ansible modules are like the function which can help you to automation.

To be continued…