{"id":213,"date":"2023-09-30T10:02:48","date_gmt":"2023-09-30T10:02:48","guid":{"rendered":"https:\/\/www.netasic.com\/?p=213"},"modified":"2026-05-18T15:26:42","modified_gmt":"2026-05-18T15:26:42","slug":"first-steps-with-network-automation-using-ansibleansible-ad-hoc-command-and-first-playbook","status":"publish","type":"post","link":"https:\/\/www.netasic.com\/index.php\/2023\/09\/30\/first-steps-with-network-automation-using-ansibleansible-ad-hoc-command-and-first-playbook\/","title":{"rendered":"First Steps with Network Automation using Ansible"},"content":{"rendered":"<h3>I have collected some good examples of ad hoc command with specific modules below.<\/h3>\n<p><span style=\"color: #333333;\">EXAMPLE 01:<\/span><\/p>\n<pre><strong><span style=\"color: #000080;\">ansible@virtualbox:~\/Desktop$ ansible localhost -m find -a \"paths=\/var\/log\/ file_type=directory\"<\/span><\/strong><\/pre>\n<p><span style=\"color: #333333;\">EXAMPLE 02:<\/span><\/p>\n<p>Criteria:<\/p>\n<p>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.<\/p>\n<p>Link here: <a href=\"https:\/\/docs.ansible.com\/ansible\/2.8\/modules\/find_module.html#find-module\">https:\/\/docs.ansible.com\/ansible\/2.8\/modules\/find_module.html#find-module<\/a><\/p>\n<p>Find interesting example:<\/p>\n<pre><span style=\"color: #000080;\">- name: Recursively find \/tmp files older than 4 weeks and equal or greater than 1 megabyte\r\nfind:\r\npaths: \/tmp\r\nage: 4w\r\nsize: 1m\r\nrecurse: yes<\/span><\/pre>\n<p>and based on parameters above you can create ad hoc command for ansible:<\/p>\n<pre><strong><span style=\"color: #000080;\">ansible@virtualbox:~\/Desktop$ sudo ansible localhost -m find -a \"paths=\/var\/log age=4w size=1m recurse=yes\"<\/span><\/strong><\/pre>\n<p><span style=\"color: #333333;\">EXAMPLE 03:<\/span><\/p>\n<pre><span style=\"color: #000080;\"><strong>ansible@virtualbox:~\/Desktop$ ansible localhost -m find -a \"paths=\/var\/log\/ patterns='*.log' size=10m\"<\/strong>\r\nlocalhost | SUCCESS =&gt; {\r\n\"ansible_facts\": {\r\n\"discovered_interpreter_python\": \"\/usr\/bin\/python3\"\r\n},\r\n\"changed\": false,\r\n\"examined\": 64,\r\n\"files\": [],\r\n\"matched\": 0,\r\n\"msg\": \"All paths examined\",\r\n\"skipped_paths\": {}\r\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\"><strong>Simply example of Ansible playbook:<\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #000080;\"><strong>ansible@virtualbox:~\/Desktop$ cat first_playbook.yml<\/strong><\/span>\r\n<span style=\"color: #000080;\">---<\/span>\r\n<span style=\"color: #000080;\">- hosts: router<\/span>\r\n<span style=\"color: #000080;\">gather_facts: false<\/span>\r\n<span style=\"color: #000080;\">connection: network_cli<\/span>\r\n\r\n<span style=\"color: #000080;\">tasks:<\/span>\r\n<span style=\"color: #000080;\">- name: run show version on ios device<\/span>\r\n<span style=\"color: #000080;\">ios_command:<\/span>\r\n<span style=\"color: #000080;\">commands:<\/span>\r\n<span style=\"color: #000080;\">- show version | in IOS<\/span>\r\n<span style=\"color: #000080;\">register: output<\/span>\r\n\r\n\r\n<span style=\"color: #000080;\">- name: show output<\/span>\r\n<span style=\"color: #000080;\">debug:<\/span>\r\n<span style=\"color: #000080;\">msg: \"{{ output }}\"<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>Executing above playbook:<\/p>\n<pre><span style=\"color: #000080;\"><strong>ansible@virtualbox:~\/Desktop$ ansible-playbook first_playbook.yml --user=cisco --ask-pass<\/strong><\/span>\r\n<span style=\"color: #000080;\">SSH password:<\/span>\r\n\r\n<span style=\"color: #000080;\">PLAY [router] ***************************************************************************************************************************<\/span>\r\n\r\n<span style=\"color: #000080;\">TASK [run show version on ios device] ***************************************************************************************************<\/span>\r\n<span style=\"color: #000080;\">[WARNING]: ansible-pylibssh not installed, falling back to paramiko<\/span>\r\n<span style=\"color: #000080;\">[WARNING]: ansible-pylibssh not installed, falling back to paramiko<\/span>\r\n<span style=\"color: #000080;\">[WARNING]: ansible-pylibssh not installed, falling back to paramiko<\/span>\r\n<span style=\"color: #000080;\">ok: [10.0.0.1]<\/span>\r\n<span style=\"color: #000080;\">ok: [10.0.0.2]<\/span>\r\n<span style=\"color: #000080;\">ok: [10.0.0.10]<\/span>\r\n\r\n<span style=\"color: #000080;\">TASK [show output] **********************************************************************************************************************<\/span>\r\n<span style=\"color: #ff0000;\">ok: [10.0.0.1] =&gt; {\r\n\"msg\": {\r\n\"changed\": false,\r\n\"failed\": false,\r\n\"stdout\": [\r\n\"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)\"\r\n],\r\n\"stdout_lines\": [\r\n[\r\n\"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)\"<\/span>\r\n<span style=\"color: #000080;\">]<\/span>\r\n<span style=\"color: #000080;\">]<\/span>\r\n<span style=\"color: #000080;\">}<\/span>\r\n<span style=\"color: #000080;\">}<\/span>\r\n<span style=\"color: #ff9900;\">ok: [10.0.0.2] =&gt; {\r\n\"msg\": {\r\n\"changed\": false,\r\n\"failed\": false,\r\n\"stdout\": [\r\n\"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)\"\r\n],\r\n\"stdout_lines\": [\r\n[\r\n\"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)\"<\/span>\r\n<span style=\"color: #000080;\">]<\/span>\r\n<span style=\"color: #000080;\">]<\/span>\r\n<span style=\"color: #000080;\">}<\/span>\r\n<span style=\"color: #000080;\">}<\/span>\r\n<span style=\"color: #99cc00;\">ok: [10.0.0.10] =&gt; {\r\n\"msg\": {\r\n\"changed\": false,\r\n\"failed\": false,\r\n\"stdout\": [\r\n\"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)\"\r\n],\r\n\"stdout_lines\": [\r\n[\r\n\"Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)\"\r\n]<\/span>\r\n<span style=\"color: #000080;\">]<\/span>\r\n<span style=\"color: #000080;\">}<\/span>\r\n<span style=\"color: #000080;\">}<\/span>\r\n\r\n<span style=\"color: #000080;\">PLAY RECAP ******************************************************************************************************************************<\/span>\r\n<span style=\"color: #000080;\">10.0.0.1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 <\/span>\r\n<span style=\"color: #000080;\">10.0.0.10 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 <\/span>\r\n<span style=\"color: #000080;\">10.0.0.2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/span>\r\n\r\n<\/pre>\n<p>After executing any playbook we are getting summary of tasks, which launched against specify nodes in my case:<\/p>\n<pre><span style=\"color: #000080;\"><strong>ansible@virtualbox:~$ cat \/etc\/ansible\/hosts\r\n<\/strong>[...]<\/span>\r\n<span style=\"color: #000080;\">[router]<\/span>\r\n<span style=\"color: #000080;\">10.0.0.1<\/span>\r\n<span style=\"color: #000080;\">10.0.0.2<\/span>\r\n<span style=\"color: #000080;\">10.0.0.10<\/span>\r\n<span style=\"color: #000080;\">[...]<\/span><\/pre>\n<p>After executing a playbook, the summary is provided under &#8216;PLAY RECAP,&#8217; and the status of specific operations can be one of the following:<\/p>\n<ol>\n<li>&#8216;ok&#8217;: Indicates that the operation was successful.<\/li>\n<li>&#8216;changed&#8217;: Shows that the operation resulted in a change on the target node.<\/li>\n<li>&#8216;unreachable&#8217;: Suggests that Ansible couldn&#8217;t connect to the target node.<\/li>\n<li>&#8216;failed&#8217;: Implies that the operation encountered an error during execution.<\/li>\n<\/ol>\n<p>These statuses help in quickly assessing the outcome of each task in the playbook.<\/p>\n<pre><span style=\"color: #000080;\">ok=2            <em>#my status after executing above playbook<\/em><\/span>\r\n<span style=\"color: #000080;\">changed=0 <\/span>\r\n<span style=\"color: #000080;\">unreachable=0 <\/span>\r\n<span style=\"color: #000080;\">failed=0 <\/span>\r\n<span style=\"color: #000080;\">skipped=0 <\/span>\r\n<span style=\"color: #000080;\">rescued=0 <\/span>\r\n<span style=\"color: #000080;\">ignored=0 <\/span><\/pre>\n<h3><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>I have collected some good examples of ad hoc command with specific modules below. EXAMPLE 01: ansible@virtualbox:~\/Desktop$ ansible localhost -m find -a &#8220;paths=\/var\/log\/ file_type=directory&#8221; EXAMPLE 02: Criteria: I would like to find files with specific criteria in a specific directory. &hellip; <a href=\"https:\/\/www.netasic.com\/index.php\/2023\/09\/30\/first-steps-with-network-automation-using-ansibleansible-ad-hoc-command-and-first-playbook\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":30,"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":343,"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/posts\/213\/revisions\/343"}],"wp:attachment":[{"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netasic.com\/index.php\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}