Ansible: Ad hoc command and first playbook

I have collected some good examples of ad hoc command with specific modules below.

EXAMPLE 01:

ansible@virtualbox:~/Desktop$ ansible localhost -m find -a "paths=/var/log/ file_type=directory"

EXAMPLE 02:

Criteria:

I would like to find files with specific criteria in a specific directory. My conditions are as follows: the path is /tmp, the files should be older than 4 weeks, have a size above 1 MB, and I would like to use recursive searching to find all files in the directory.

Link here: https://docs.ansible.com/ansible/2.8/modules/find_module.html#find-module

Find interesting example:

- name: Recursively find /tmp files older than 4 weeks and equal or greater than 1 megabyte
find:
paths: /tmp
age: 4w
size: 1m
recurse: yes

and based on parameters above you can create ad hoc command for ansible:

ansible@virtualbox:~/Desktop$ sudo ansible localhost -m find -a "paths=/var/log age=4w size=1m recurse=yes"

EXAMPLE 03:

ansible@virtualbox:~/Desktop$ ansible localhost -m find -a "paths=/var/log/ patterns='*.log' size=10m"
localhost | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"examined": 64,
"files": [],
"matched": 0,
"msg": "All paths examined",
"skipped_paths": {}
}

 

Simply example of Ansible playbook:

 

ansible@virtualbox:~/Desktop$ cat first_playbook.yml
---
- hosts: router
gather_facts: false
connection: network_cli

tasks:
- name: run show version on ios device
ios_command:
commands:
- show version | in IOS
register: output


- name: show output
debug:
msg: "{{ output }}"

 

Executing above playbook:

ansible@virtualbox:~/Desktop$ ansible-playbook first_playbook.yml --user=cisco --ask-pass
SSH password:

PLAY [router] ***************************************************************************************************************************

TASK [run show version on ios device] ***************************************************************************************************
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
ok: [10.0.0.1]
ok: [10.0.0.2]
ok: [10.0.0.10]

TASK [show output] **********************************************************************************************************************
ok: [10.0.0.1] => {
"msg": {
"changed": false,
"failed": false,
"stdout": [
"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)"
],
"stdout_lines": [
[
"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)"
]
]
}
}
ok: [10.0.0.2] => {
"msg": {
"changed": false,
"failed": false,
"stdout": [
"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)"
],
"stdout_lines": [
[
"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)"
]
]
}
}
ok: [10.0.0.10] => {
"msg": {
"changed": false,
"failed": false,
"stdout": [
"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)"
],
"stdout_lines": [
[
"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)"
]
]
}
}

PLAY RECAP ******************************************************************************************************************************
10.0.0.1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 
10.0.0.10 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 
10.0.0.2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

After executing any playbook we are getting summary of tasks, which launched against specify nodes in my case:

ansible@virtualbox:~$ cat /etc/ansible/hosts
[...]
[router]
10.0.0.1
10.0.0.2
10.0.0.10
[...]

After executing a playbook, the summary is provided under ‘PLAY RECAP,’ and the status of specific operations can be one of the following:

  1. ‘ok’: Indicates that the operation was successful.
  2. ‘changed’: Shows that the operation resulted in a change on the target node.
  3. ‘unreachable’: Suggests that Ansible couldn’t connect to the target node.
  4. ‘failed’: Implies that the operation encountered an error during execution.

These statuses help in quickly assessing the outcome of each task in the playbook.

ok=2            #my status after executing above playbook
changed=0 
unreachable=0 
failed=0 
skipped=0 
rescued=0 
ignored=0 

END.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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