Ansible Commands

Published on Author gryzliLeave a comment

Here I will describe some ansible related commands, which I use in my day-to-day ansible usage.

 

ansible-playbook

Execute test_job.yml playbook

 

Ansible ansible-playbook limit playbook execution to a certain hosts only

Sometime you may want to execute a given playbook (or part of it), only on certain hosts. Instead of making different inventory hosts files, for every “custom” sets of hostnames, you could try these techniques:

  • Use “–limit” option to tell which hosts to operate on:
    # Execute the playbook on host1

     

  • Use “–limit” option to limit to a “multiple”/”a set of” hosts
    # Execute the playbook on host1,host2,host3 hosts

     

  • Use “–limit” option to limit to a “multiple”/”a set of” hosts using ranges
    # Execute the playbook on “host10 through host20” or “host5 through host15”

     

  • Use “–limit” option and supply a filename with subset of hostnames:

     

If you are not sure what hosts “–limit” will work on, you could use the option “–list-hosts“. By adding “–list-hosts” you will get a list of hosts on which the playbook will be executed.

 

Check which hosts are going to be included in a given execution by using “–list-hosts”

If you you use “–list-hosts”, ansible wont execute any of the defined task, but will just print the hosts you are going to work with.

Example command line looks like this:

Or if you are using just ansible to execute a simple command

 

 

Ansible ansible-playbook execute only certain tasks from a playbook

Recently I needed to execute only a give task from a predefined playbook. Ansible has some pretty nice feature for doing such a thing which is called “tags”.

In summary, for every task you can define “tags“, which are something like “keywords“. Later one you could use those defined “tags”  and execute only task which tags are matching (or not matching) a given criteria.

 

Start ansible playbook execution from a certain task by using –start-at-task

Sometimes you may need to execute certain playbook from a given task to the end. For example let say you have defined your playbook with following tasks:

 

You may need to execute only the last 3 tasks from this playbook. In a such situations, the ‘–start-at-task’ options becomes very handy. Here is an example:

 

The result from executing the above command is that you will execute only Task3, Task4 and Task5, in short everything from Task3 to the end.

 

Using –step to execute custom tasks from ansible playbook

Another usefull options for custom task execution is the using of “–step” argument. While using this argument, Ansible will ask you on every single task (in the playbook) if you really want to execute it.

 

 

Some bonus stuff:

  • You could have multiple tasks with SAME tag. This will lead to the execution of all the tasks by providing the “tag
  • You could make a task execute in ALL CONDITIONS, independently of the tags supplied. This could be done by adding:

 

Examples

Example on using –tags and –skip-tags

main.yml

Now if we want to copy ONLY FILE1:

If we want to copy everything BUT FILE1

 

Check which tasks will be executed from the given “–tags / –skip-tags”:

 

Example on hash loop in ansible

 

More about ansible loops, can be found in the official docs:

Ansible Loops

 

Ansible – Define hash variable and use it later

If you want to define HASH variables for later use, you can use the following syntaxis:

vim group_vars/all

 

You can then use this in your tasks, by the following convention:

{{eth0_ip.ip}}

{{eth0_ip.mask}}

{{eth0_ip.broadcast}}

 

Ansible – Assign output of a shell command to a ansible variable for a later use

The following code will execute the shell command “hostname -s” and then assign it’s output and return code to:

output         ==> host_var.stdout
return code ==> host_var.rc

Add this to your tasks/main.yml :

Now you could reference the host_var:

 

 

 

ansible

Execute simple command on a given host

Execute the command:

 

Execute using the “shell” module:

 

Copy files to a remote host

 

Get all facts about a host

 

 

 

Usefull Articles

https://serversforhackers.com/an-ansible-tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.