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 | [web1] |
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 | - name: hello loop |
after runing
1 | anna@ansible-controller:~/ansible-code/inventory/loop$ ansible-playbook helloloop.yml |
with_nested
is used in multiple loops.
1 | - name: nested loop |
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 | - name: test conditon |
after running,
1 | anna@ansible-controller:~/ansible-code/inventory/condition$ ansible-playbook when.yml |
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…