It focuses on reaching a desired state
component
- Inventories
- Playbooks
- Plays
- Tasks
- Modules
Inventories
It is the list of nodes/hosts, can be static or dynamic list from cloud provider like AWS
We can grouped hosts based on location, function & …
We have variables that we can set attribute to hosts like service port, timeout & ….
for creation we have 2 options:
1- simple text
2- inventory script
We can add path of inventory to ansible.cfg file in local directory or /etc/ansible/ansible.cfg
local directory has priority over default ansible.cfg file
–limit: to limit execution of command or playbook on the inventory hosts
Playbook
It is yaml formated
Describe the end state of sth.
Meant to be reusable, so we can keep them under source control to:
1- track changes
2- make them easily available
Play
Multi play is inside a playbook
each play has target (hosts) which include remote user/sudo and execute tasks
Tasks
Each task runs one or more modules with parameters and returned value for that module.
Modules
We can find help (list) for modules on the website or ansible-doc <copy> command
Modules make sure that requested state is achieved as indicate with parameters and if not doing what is need.
example:yum – mysql-db – ping
Conditionals
is applied to each task. e.g. yum: weather or not to install a package
parameters
let you add information to modify the task
Modules returne value about result of the modules action e.g. failed, skipped, changed.
Roles, Import, Include
break the playbook into smaller pieces that can be used in (include) in multiple playbook.
Example
- name: Create web server
hosts: ws
remote_user: joe
become_method: sudo
become: yes
tasks:
- name: Install httpd
yum:
name: httpd
state: present
- name: Check that httpd has started
service:
name: httpd
state: started
- name: Install firewalld
yum:
name: firewalld
state: present
- name: Firewall access to https
firewalld:
service: http
permanent: yes
state: enabled
- name: Restart the firewalld service to load in the firewall changes
service:
name: firewalld
state: restarted
Run the playbook
ansible-playbook command to run the playboook-C – test playbook before running-v – verboseansible-palybook -v simple-web.yaml
ad-hoc Command
Indicate which command & options you want to run on a group of nodes as a one-time activity
example:ansible [ws] -u <user> -m ping