Ansible's roles allow grouping the content, which can be reused and easily share with other users. The role consists of vars, files, tasks, and other artifacts in a standard directory structure which have these subdirectories.
We can use roles in the playbook or task with the help of Include and Import. To know more about Import/Include, you can check here
At Playbook level with the roles
At task level with include_roles
At task level with import_roles
Roles at playbook level are imported statically and executes the playbook in this order
Roles listed in the playbook
Tasks define in the playbook and any handler triggered by the tasks
Post tasks define after the role
- hosts: webservers
Task with Include
- hosts: all
- name: Use a dynamic roles
Task with Import
- hosts: all
- name: Use a static roles
- hosts: all
- name: Include the some_role role
when: "ansible_facts['os_family'] == 'Ubuntu'"
Ansible's roles are really helpful to group the similar content and use in the different playbook and give the feasibility to the user to share with other which saves a lot of work. It's worth using them and shares your roles in the community to help others.
In Ansible playbooks are a collection of different tasks. It's a good idea to break the tasks into the different files, which make it easier to include/import these task in different playbooks. Now the question is when to use include or import and how these two are different from each other?
Ansible provides four distributed
Including variables, tasks, or role adds them into the playbook dynamically. This means when Ansible processes these files as they come up they are included in the current playbook as its variable, task, or role. So, these can be affected by the previous tasks.
Playbook's can not be used with the include
Importing task, playbook, or role add them into playbook statically. Ansible pre-processes these files before it runs any task in the playbook, which means these are not affected by other tasks.
Tip: Import variables if you want to use these more than once in the playbook.
Import and Include differ with the way Ansible loads these files into the playbook. So, it is good to use the one which best fits your use case.
Handling errors with blocks
Ansible let you control errors in a block with rescue and always section. In rescue, we can define tasks which we want to run when an earlier task in a block fails. You can also think of rescue as except and always as finally in other languages to handle errors.
- name: tasks to be run
- name: task 1
- name: task 2
- name: task 3
- name: Run when one of the above task fails
- name: Always run
Ansible provide ignore, rescue, always, failed_when to easily handle the behavior of playbook when we face any errors. We can use these commands/settings to gracefully record errors and take specific action.
Ansible have two special tags, never and always. If you add always tag to task, play, Ansible will always run the task or play, unless explicitly asked to skip with the help of (—skip-tags always)
On the other hand, never tag if assign to a task or play, Ansible will skip that task or play unless you explicitly asked it (—tags never).
Running the Playbook with tags
will run tasks with tag git : ansible-playbook example.yml --tags "git"
will not run tasks with tag git : ansible-playbook example.yml --skip-tags "git"
list all the tags : ansible-playbook example.yml --list-tags
run all tasks, ignore tags (default behavior): --tags all
run only tasks with either the tag tag1 or the tag tag2: --tags [tag1, tag2]
run all tasks, except those with either the tag tag3 or the tag tag4:
--skip-tags [tag3, tag4]
run only tasks with at least one tag: --tags tagged
run only tasks with no tags: --tags untagged
That's all about the Ansible tags. Now go and tag your tasks :).
While running your Ansible playbook, we can have the option of passing the argument from the command line. With the help of this, we define a generic playbook and use command line arguments to control the play. Let see how
Let's first start with the playbook, where we have defined the variable x is the playbook.
Inventory(Hosts) file contains the information of the Ansible nodes. We can group based on our use case like web servers, DB servers and uses these groups to execute commands on them. But you have to create a group in the inventory file, let see how to do that.
What is Ansible
Ansible is a simple IT automation tool that make application
and system easier to deploy and maintain. With the help of Ansible, you can automate task like network configuration, code deployment and cloud management. Ansible is an agent less system, which means that you don't need to install any software on the client system.
Inventory file is the one which contains the lists of the host servers on which Ansible works, it can be the IP Addresses, Domain name ...
It contains the IP addresses or Fully qualified domain name of the node servers which you want to configure.
This file contains the configuration for the Ansible, like the path of the Inventory file and values for the default variable in the Ansible
Ansible's nodes are the client system on which we want to execute, deploy or maintain the code(servers on which we want to work).
Ansible engine/master/controller from which we manage all the server nodes.
I came to see this warning a few days back when I was seeing the git diff of a file in reaction to this I open the file and hit enter at the last line to my surprise the warning remains still the same.
What is a new line?
The new line is usually \n, aka (CR or CRLF) at the end of the file. It's helpful to identify the last byte of the file.
Why it is good to have a new line at the end of the file?
A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character.
Since this is a “shall” clause, we must emit a diagnostic message for a violation of this rule.
So, it turns out that, according to POSIX, every text file should end with a \n, or “newline” (not “a new line”) character. This acts as the eol, or the “end of line” character. It is a line “terminator”.
It's helpful to identify the end of file.
How to automatically add one with your favorite editor?
In your favorite editor, you can add newline automatically like this.
Emacs : Add (setq require-final-newline t) to your .emacs or .emacs.d/init.el file.
It is an abstract machine that can be in exactly one of a finite number of states at any given time. A state machine is just a specification of how to handle events. It consists of a set of states, one of which is the current state. For each state, we list the events that are significant to that state. For each of those events, we define the new current state of the system.
Here we have defined the states Stable, Critical, Warning and events DB down, DB up, and N/W down. In this case, we have a stable state so whenever our database goes down or the network goes down the state changes to critical/warning. We can link some logic with these states to perform any action for transition.
This logic can help in designing a good program around a problem statement that will have less coupled code and easy to maintain.