{"id":499,"date":"2020-07-14T17:53:00","date_gmt":"2020-07-14T16:53:00","guid":{"rendered":"https:\/\/zed-network.fr\/?p=499"},"modified":"2020-08-24T19:04:40","modified_gmt":"2020-08-24T18:04:40","slug":"automate-cisco-ucs-with-ansible","status":"publish","type":"post","link":"https:\/\/zed-network.fr\/?p=499","title":{"rendered":"How to automate Cisco UCS with Ansible playbook"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To use ansible with Cisco UCS Manager, you need to install the SDK on your ansible server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@c86d023821ff:~# pip install ucsmsdk\nCollecting ucsmsdk\nDownloading https:\/\/files.pythonhosted.org\/packages\/f7\/f9\/280d7cea9e37ed183d694f5d2e8505b5eacfdbad709eba68fc32a5ed2bcf\/ucsmsdk-0.9.10.tar.gz (4.2MB)\n100% |################################| 4.2MB 306kB\/s\nCollecting pyparsing (from ucsmsdk)\nDownloading https:\/\/files.pythonhosted.org\/packages\/8a\/bb\/488841f56197b13700afd5658fc279a2025a39e22449b7cf29864669b15d\/pyparsing-2.4.7-py2.py3-none-any.whl (67kB)\n100% |################################| 71kB 5.2MB\/s\nRequirement already satisfied: setuptools in \/usr\/lib\/python2.7\/dist-packages (from ucsmsdk) (40.8.0)\nRequirement already satisfied: six in \/usr\/lib\/python2.7\/dist-packages (from ucsmsdk) (1.12.0)\nBuilding wheels for collected packages: ucsmsdk\nRunning setup.py bdist_wheel for ucsmsdk \u2026 done\nStored in directory: \/root\/.cache\/pip\/wheels\/ac\/a2\/a9\/5c39875aca61b780d8d94690f22b54237452d9fc290756781f\nSuccessfully built ucsmsdk\nInstalling collected packages: pyparsing, ucsmsdk\nSuccessfully installed pyparsing-2.4.7 ucsmsdk-0.9.10\n\nroot@c86d023821ff:~# pip list | grep ucs\nucsmsdk 0.9.10<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then you can create a simple inventory file with the UCS Manager IP. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@c86d023821ff:~# cat inv_ucs\n[ucs]\n10.0.100.162<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then you can create a simple playbook to add one vlan:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@c86d023821ff:~# cat ucs_vlan.yml\n---\n- name: ENSURE APPLICATION CONFIGURATION EXISTS\n  hosts: ucs\n  connection: local\n  gather_facts: False\n\n  tasks:\n\n  - name: Configure VLAN\n    ucs_vlans:\n      hostname: 10.0.100.162\n      username: ucspe\n      password: ucspe\n      name: TheVlan11\n      id: '11'\n      native: 'no'\n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And finally you can run it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@c86d023821ff:~# ansible-playbook ucs_vlan.yml -i inv_ucs\nPLAY [ENSURE APPLICATION CONFIGURATION EXISTS] *\nTASK [Configure VLAN] **\n[WARNING]: Platform linux on host 10.0.100.162 is using the discovered Python interpreter at \/usr\/bin\/python, but\nfuture installation of another Python interpreter could change this. See\nhttps:\/\/docs.ansible.com\/ansible\/2.9\/reference_appendices\/interpreter_discovery.html for more information.\nchanged: [10.0.100.162]\nPLAY RECAP <strong><em>*<\/em><\/strong>\n10.0.100.162 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now your vlan ID 11 is available on the UCS Fabric Interconnect.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"956\" height=\"516\" src=\"https:\/\/zed-network.fr\/wp-content\/uploads\/image-13.png\" alt=\"\" class=\"wp-image-500\" srcset=\"https:\/\/zed-network.fr\/wp-content\/uploads\/image-13.png 956w, https:\/\/zed-network.fr\/wp-content\/uploads\/image-13-300x162.png 300w, https:\/\/zed-network.fr\/wp-content\/uploads\/image-13-768x415.png 768w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the module ansible ucs_vlan_find to get all vlans:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@c86d023821ff:~# cat ucs_vlan.yml\n---\n- name: ENSURE APPLICATION CONFIGURATION EXISTS\n  hosts: ucs\n  connection: local\n  gather_facts: False\n\n  tasks:\n\n  - name: Get All Vlans\n    ucs_vlan_find:\n      hostname: 10.0.100.162\n      username: ucspe\n      password: ucspe\n      pattern: '.'\n    register: vlans\n    tags:\n    - showvlan\n\n  - name: Display vlans\n    debug:\n      var: vlans\n    tags:\n    - showvlan\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@c86d023821ff:~# ansible-playbook ucs_vlan.yml -i inv_ucs --tags showvlan\nPLAY [ENSURE APPLICATION CONFIGURATION EXISTS]\nTASK [Get All Vlans]\n[WARNING]: Platform linux on host 10.0.100.162 is using the discovered Python interpreter at \/usr\/bin\/python, but future installation of\nanother Python interpreter could change this. See https:\/\/docs.ansible.com\/ansible\/2.9\/reference_appendices\/interpreter_discovery.html for\nmore information.\nok: [10.0.100.162]\nTASK [Display vlans]\nok: [10.0.100.162] =&gt; {\n\"vlans\": {\n\"ansible_facts\": {\n\"discovered_interpreter_python\": \"\/usr\/bin\/python\"\n},\n\"changed\": false,\n\"failed\": false,\n\"vlan_list\": [\n{\n\"id\": \"1\",\n\"name\": \"default\"\n},\n{\n\"id\": \"5\",\n\"name\": \"human-resource\"\n},\n{\n\"id\": \"1\",\n\"name\": \"default\"\n},\n{\n\"id\": \"3\",\n\"name\": \"finance\"\n},\n{\n\"id\": \"5\",\n\"name\": \"human-resource\"\n},\n{\n\"id\": \"1\",\n\"name\": \"default\"\n},\n{\n\"id\": \"3\",\n\"name\": \"finance\"\n},\n{\n\"id\": \"42\",\n\"name\": \"NewVlan42\"\n},\n{\n\"id\": \"10\",\n\"name\": \"vlan10\"\n},\n{\n\"id\": \"11\",\n\"name\": \"TheVlan11\"\n}\n],\n\"warnings\": [\n\"Platform linux on host 10.0.100.162 is using the discovered Python interpreter at \/usr\/bin\/python, but future installation of another Python interpreter could change this. See https:\/\/docs.ansible.com\/ansible\/2.9\/reference_appendices\/interpreter_discovery.html for more information.\"\n]\n}\n}\nPLAY RECAP **\n10.0.100.162 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To use ansible with Cisco UCS Manager, you need to install the SDK on your ansible server. root@c86d023821ff:~# pip install ucsmsdk Collecting ucsmsdk Downloading https:\/\/files.pythonhosted.org\/packages\/f7\/f9\/280d7cea9e37ed183d694f5d2e8505b5eacfdbad709eba68fc32a5ed2bcf\/ucsmsdk-0.9.10.tar.gz (4.2MB) 100% |################################| 4.2MB 306kB\/s Collecting pyparsing (from ucsmsdk) Downloading https:\/\/files.pythonhosted.org\/packages\/8a\/bb\/488841f56197b13700afd5658fc279a2025a39e22449b7cf29864669b15d\/pyparsing-2.4.7-py2.py3-none-any.whl (67kB) 100% |################################| 71kB 5.2MB\/s Requirement already satisfied: setuptools in \/usr\/lib\/python2.7\/dist-packages (from ucsmsdk) (40.8.0) Requirement already satisfied: six in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[28,27,4,32],"tags":[20],"class_list":["post-499","post","type-post","status-publish","format-standard","hentry","category-ansible","category-automation","category-cisco","category-ucs-automation","tag-cisco"],"_links":{"self":[{"href":"https:\/\/zed-network.fr\/index.php?rest_route=\/wp\/v2\/posts\/499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zed-network.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zed-network.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zed-network.fr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zed-network.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=499"}],"version-history":[{"count":5,"href":"https:\/\/zed-network.fr\/index.php?rest_route=\/wp\/v2\/posts\/499\/revisions"}],"predecessor-version":[{"id":540,"href":"https:\/\/zed-network.fr\/index.php?rest_route=\/wp\/v2\/posts\/499\/revisions\/540"}],"wp:attachment":[{"href":"https:\/\/zed-network.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zed-network.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zed-network.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}