Ansible Commands

Published on Author gryzli

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

# --check  --> Only check, don't apply any changes
# -i  --> Tell the playbook to use "my_custom_hosts_file" as an inventory list

ansible-playbook --check -i my_custom_hosts_file test_job.yml

 

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

    ansible-playbook -i all_hosts.inventory some_playbook.yml --limit "host1"

     

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

    ansible-playbook -i all_hosts.inventory some_playbook.yml --limit "host1,host2,host3"

     

  • 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”

    ansible-playbook -i all_hosts.inventory some_playbook.yml --limit host[10-20] 
    # OR 
    ansible-playbook -i all_hosts.inventory some_playbook.yml --limit host[5-15] 
    

     

  • Use “–limit” option and supply a filename with subset of hostnames:
    ansible-playbook -i all_hosts.inventory some_playbook.yml --limit @/path/to/file
    
    # File contents
    cat /path/to/file
    host1
    host2
    ....

     

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:

ansible-playbook some_playbook.yml   -i hosts/lot_of.hosts  --limit all[0-2] --list-hosts 

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

ansible -i hosts/lot_of.hosts all[0-2] --list-hosts 

 

 

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:

- name: Task1 
- name: Task2
- name: Task3 
- name: Task4 
- name: Task5 

 

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:

ansible-playbook your_playbook.yml --start-at-task='Task3'

 

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:
    always_run: yes

 

Examples

Example on using –tags and –skip-tags

main.yml

tasks:

- name: Copy FILE1
  copy: src=/root/work/FILE1 dest=/root/work/FILE1
  tags:
   - file1

- name: Copy FILE2
  copy: src=/root/work/FILE2 dest=/root/work/FILE2
  tags:
   - file2

- name: Copy FILE2
  copy: src=/root/work/FILE3 dest=/root/work/FILE3
  tags:
   - file3

Now if we want to copy ONLY FILE1:

ansible-playbook main.yml --tags "file1"

If we want to copy everything BUT FILE1

ansible-playbook main.yml --skip-tags "file1"

 

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

ansible-playbook main.yml --skip-tags "file1" --list-tags

# OR 

ansible-playbook main.yml --tags "file1" --list-tags

 

Example on hash loop in ansible

 

- name: vim files
  synchronize:
    src=/absolute/repo/path/roles/common/files/{{ item.src }}
    dest={{ item.dst }}
  with_items:
    - {src: vim/bundle, dst: /home/jefdaj/.vim  }
    - {src: vim/vimrc , dst: /home/jefdaj/.vimrc}
    - {src: vim/bundle, dst: /root/.vim         }
    - {src: vim/vimrc , dst: /root/.vimrc       }

- name: vim permissions
  file: path={{ item.pth }} owner={{ item.own }} group={{ item.grp }} recurse=yes
  with_items:
    - {pth: /root/.vim         , own: root  , grp: root  }
    - {pth: /root/.vimrc       , own: root  , grp: root  }
    - {pth: /home/jefdaj/.vim  , own: jefdaj, grp: jefdaj}
    - {pth: /home/jefdaj/.vimrc, own: jefdaj, grp: jefdaj}

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

eth0_ip: 
  {
  "ip":192.168.1.1,
  "mask":255.255.255.0,
  "broadcast":192.168.1.255,
  }

 

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 :

# This must be added in your TASKS
- name: Get the output of hostname command
  shell: hostname -s
  register: host_var

Now you could reference the host_var:

# This will add:
# HOSTNAME=some_host
# in the /tmp/some_test_file file
- name: Use the host_var variable
  lineinfile: >
    dest=/tmp/some_test_file
    line=HOSTNAME={{host_var.stdout}}

 

 

 

ansible

Execute simple command on a given host

Execute the command:

# Execute uptime 
# all --> Execute it on all hosts
# -i  --> Inventory file (containing hosts)
# -f 10 --> Set parallel execution to 10
# -a "/bin/uptime"
ansible -i my_inventory all -a "/bin/uptime" -f 10

 

Execute using the “shell” module:

# some_host - The host where command will be executed
ansible -i my_inventory some_host -m shell -a 'echo $TERM'

 

Copy files to a remote host

$ ansible -i my_hosts -m copy -a "src=/path/to/some/local/file dest=/path/to/destination/file"

 

Get all facts about a host

# Gather all facts
ansible-playbook -i inventory host1 -m setup

# Or get just specific facts
ansible-playbook -i inventory host1 -m setup -a "filter=some_string*"

 

 

 

Usefull Articles

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