Docs: Update to APPC Ansible Adaptor doc
[appc/deployment.git] / docs / APPC Ansible Adapter / APPC Ansible Adapter.rst
1 .. ============LICENSE_START==========================================
2 .. ===================================================================
3 .. Copyright © 2017-2020 AT&T Intellectual Property. All rights reserved.
4 .. ===================================================================
5 .. Licensed under the Creative Commons License, Attribution 4.0 Intl.  (the "License");
6 .. you may not use this documentation except in compliance with the License.
7 .. You may obtain a copy of the License at
8 .. 
9 ..  https://creativecommons.org/licenses/by/4.0/
10 .. 
11 .. Unless required by applicable law or agreed to in writing, software
12 .. distributed under the License is distributed on an "AS IS" BASIS,
13 .. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 .. See the License for the specific language governing permissions and
15 .. limitations under the License.
16 .. ============LICENSE_END============================================
17 .. ECOMP is a trademark and service mark of AT&T Intellectual Property.
18
19 ====================
20 APPC Ansible Adapter
21 ====================
22
23 This wiki provides documentation regarding the design, capabilities and
24 usage of the Ansible Extension for APP-C. Ansible_ is a an open-source
25 VNF management framework that allows provide an almost cli like set of
26 tools in a structured form. It is agentless in that the target VNF need
27 not have any additional software other than:
28
29 a) SSH server
30 b) Python >= 2.7 (Note: Since Python 2 support ends in 2020, ansible docker build in ONAP Frankfurt release has been upgraded to Python 3. Python 3 is highly recommendation version.)
31 c) Any necessary software that is specific to the VNF to run its functions. 
32
33 Any action (e.g configure, restart, health check etc) can be
34 executed on the VNF by constructing a **playbook (or set of playbooks)**
35 that is executed by an Ansible agent on the VNF via SSH.
36
37 The Ansible Extension for APP-C allows management of VNFs that support Ansible
38 through the following three additions :
39
40  - **An APP-C/Ansible Server interface:** Ansible libraries are written in python and hence cannot be executed natively from within the APP-C Karaf container. Instead, the design calls for an **Ansible Server** that can execute the Ansible playbooks and exposes a **REST** interface that is compliant with requirements of APP-C. These requirements are documented as the Server API Interface that any compliant Ansible Server must support. Exact implementation of the Ansible Server is left open and does not affect APP-C operations as long as the server follows the interface. For purposes of evaluation, a reference web server that implements this APP-C/Ansible Server interface has been developed and the code is available from the App-C ONAP repository under *appc-adapters/appc-ansible-adapter/appc-ansible-example-server*.
41
42  - **An APP-C Ansible adapter:** The ansible adapter is an OSGI bundle in the APP-C Karaf container that interacts with the Ansible Server . It is essentially a set of REST calls that performs two actions, submit request for a playbook to be executed, and if required get the results of the playbook after execution (if in synchronous mode).
43
44  - **Ansible Directed Graph (DG):** The Ansible Directed graph is generic DG that can be used to invoke any playbook via Ansible (and hence any APP-C action, since in Ansible, VNF actions map to playbooks) corresponding to an LCM action.
45
46 The architecture design for supporting Ansible is outlined in the diagram below :
47
48 |image0|
49
50 The workflow envisioned when Application Controller receives an event is
51 as follows :
52
53 1) Application Controller receives event from the Event Bus for an LCM action.
54 2) The appropriate LCM API invokes the Dispatcher which performs the relevant lookups for A&AI and Workflow (DG information).
55 3) The dispatcher calls the DG relevant to the LCM action (for the VNF).
56 4) The DG conducts any processing of data (e.g retrieving additional information, filling templates etc) , prepares the necessary DG context variables outlined in Table 1 and then invokes the Ansible DG.
57 5) Ansible DG leverages the Ansible Adapter to interact with the Ansible Server.
58 6) Ansible Server invokes the appropriate playbook which in turn interacts with the VNF and then returns the playbook results.
59 7) Ansible Server returns results.
60 8) Ansible DG provides these results back to calling DG.
61
62 A ladder diagram of the work flow is pasted below :
63
64 |image1|
65
66 Details of each of these three (DG, Adapter and Ansible Server) are listed below :
67
68 1.  **Ansible Directed Graph (DG):** The Ansible Directed graph is the most common way an App-C developer is expected to leverage Ansible functionality, which is invoked by RPC: Generic_AnsibleDG, Version: 4.0.0. The Ansible DG is a general purpose graph that can be used to invoke and retrieve results from any playbook on an ONAP-compliant Ansible Server. The Ansible Graph,when called, expects a certain set of inputs to be provided as input and when upon completion provides results from the execution of the Ansible playbook. The Ansible
69 DG can be invoked using the following (current) naming:
70
71 +------------+----------------------+
72 | Field      | Value                |
73 +============+======================+ 
74 | module     | APPC                 |
75 +------------+----------------------+
76 | rpc        | ansible-adapter-1.0  | 
77 +------------+----------------------+
78 | version    | 2.0.1                | 
79 +------------+----------------------+
80
81 The inputs that the Ansible DG expects in DG context memory are listed below:
82
83 Table 1: Input Parameters to the Ansible Directed Graph
84
85 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
86 | Variable Name  | Description                                                     | Type                  | Comments                                                                 |
87 +================+=================================================================+=======================+==========================================================================+
88 | User           | Username to logon to Ansible Server                             |  Mandatory            | Should be provided by APPC.                                              |
89 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
90 | Password       | Password to logon to Ansible Server                             |  Mandatory            | Should be provided by APPC.                                              |
91 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
92 | AgentUrl       | The complete URL of the Ansible Server to post the request for  |  Mandatory            | Should be provided by APPC.                                              |
93 |                | execution and retrieve results (if in synchronous mode)         |                       |                                                                          |
94 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
95 | PlaybookName   | Name/identifier of playbook to run                              |  Mandatory            | To be provided in the template.                                          |
96 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
97 | Action         | The type of VNF action being requested                          |  Optional             | Provided either by APPC or Template.                                     |
98 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
99 | EnvParameters  | A JSON dictionary (stringified) listing the parameters to be    |  Optional             | Structure of the EnvParameters dictionary to be supplied in template.    |
100 |                | passed to the Ansible playbook                                  |                       | Values to be filled in by App-C based on values from payload in run-time |
101 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
102 | FileParameters | A JSON dictionary (stringified) listing file names and files to |  Optional             | Structure of the FileParameters dictionary to be supplied in template.   |
103 |                | be created for Ansible playbook                                 |                       | Values to be filled in by App-C based on values from payload in run-time |
104 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
105 | Timeout        | Time Ansible Server should wait before terminating playbook     |  Optional             | To be provided in the template.                                          |
106 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
107 | NodeList       | List of FQDNs/IP Addresses of VNF that the Ansible playbook     |  Optional             | To be provided to App-C during Runtime.                                  |
108 |                | should be executed on.                                          |  (if not supplied,    |                                                                          |
109 |                |                                                                 |  will run on server)  |                                                                          |
110 +----------------+-----------------------------------------------------------------+-----------------------+--------------------------------------------------------------------------+
111
112   The 'template' referred in the above table must be a JSON file as documented in the VNF vendor requirements and must contain the key-value pairs listed above (that are expected to be in the template). An LCM API Directed graph should fill in necessary parameters in the template, and then put the key-value pairs from the template as listed above in DG context memory before calling the Ansible DG.
113
114 Upon completion the Ansible DG sets the following variables in DG context memory
115
116 Table 2: Output Variables set by Ansible DG Variable
117
118 +-----------------------+--------------------------------------------------------------------------+
119 | Type                  | Comments                                                                 |
120 +=======================+==========================================================================+
121 | output.status.code    | Result of the request: 400 if SUCCESS , 200 if FAILURE.                  |
122 |                       |                                                                          |
123 |                       | The ansible playbook may have multiple sub-tasks, playbooks etc and may  |
124 |                       | run on multiple VMs of a host. The request is considered to fail if even |
125 |                       | one of the tasks is incomplete.                                          |
126 +-----------------------+--------------------------------------------------------------------------+
127 | output.status.message | If playbook finished, set to FINISH, if playbook terminated, set to      |
128 |                       | TERMINATED. If abnormal error, reported in message                       |
129 +-----------------------+--------------------------------------------------------------------------+
130 | output.status.results | A JSON dictionary with results corresponding to output provided by the   |
131 |                       | Ansible playbook request. This is optional (may not be present if        |
132 |                       | playbook was terminated). The results, if present, will be in the form   |
133 |                       | of a dictionary that follows the format presented in the Ansible Server  |
134 |                       | API Documentation. The document also contains examples of output.        |
135 +-----------------------+--------------------------------------------------------------------------+
136
137   *Note : The Ansible Server supports a Callback Url functionality, but it is currently not invoked by App-C Ansible Adapter or Directed Graph. If added, it is easy to change the Adapter and Ansible DG to support this.*
138
139 2.  **APP-C Ansible Adapter:** The App-C Ansible Adapter is an OSGI bundle which essentially makes REST calls to the Ansible Server. It exposes three methods that can be invoked by the Service Logic Interpreter (SLI).
140
141   a. *void reqExec(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException*: A method to invoke the test.
142
143   b. *void reqExecResult(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException*:  A method to request results of a test.
144
145   c. *void reqExecLog(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException* : A method to retreive the logs from a request (not used in the Ansible DG currently).
146
147     Currently, the Ansible DG uses only the first two (reqExec and reqExecResult) since only these two are needed to request execution of a playbook and retrieval of results. The reqExecLog is for diagnostic purposes.
148
149     In order to communicate with the Ansible Server, it is currently assumed that:
150
151     a. Credentials comprise of a username and password.
152
153     b. Communication is over https
154
155     The Ansible Adapter has three configurable parameters related to SSL certificate of the Ansible Server, which can be set from the properties file:
156
157     a. org.onap.appc.adapter.ansible.clientType. If set to "TRUST\_ALL", will accept all SSL certificates from any Ansible Server. If set to "TRUST\_CERT", will accept SSL from only those Ansible Servers whose certificate is in the trustStore keystore file. These two options can be used for development environment. Default option is to trust only well known server certificates (use in Production).
158
159     b. org.onap.appc.adapter.ansible.trustStore used to point to the keystore file
160
161     c. org.onap.appc.adapter.ansible.trustStorePasswd used to set password for keystore file
162
163 3.  **Reference Ansible Server Implementation of APPC / Ansible Interface (for testing purposes only)**
164
165     a. Overview
166
167     |image2|
168
169     b. Inventory file
170
171       The Ansible Server requires that all credentials and IP Addresses for the VNF being tested either already be present in the Server’s Database or be loaded before any playbooks are invoked. Supported credentials are user-name/password and public-key authentication. 
172
173       All VNF credentials stored in a unique file:
174
175       .. code:: bash
176
177         [host]
178         localhost ansible\_connection=local
179         ... 
180         [hostgroup1]
181         hostname11 ansible\_connection=ssh ansible\_ssh\_user=loginid11 ansible\_ssh\_pass=passwd11
182         hostname12 ansible\_connection=ssh ansible\_ssh\_user=loginid12 ansible\_ssh\_private\_key\_file=kefile12
183         ...
184         [hostgroup2]
185         hostname21 ansible\_connection=ssh ansible\_ssh\_user=loginid21 ansible\_ssh\_private\_key\_file=keyfile21
186         ...
187         [hostgroup3]
188         ...
189
190     c. Playbooks
191
192       Playbooks can either be provided as stand alone text files or gzipped tar file (playbooks with roles sub-directories) either stored in a local file.
193
194       Naming convention: anything\_LCM@M.mn.{yml,tar.gz} where version number M is a digit and mn are subversion number digits.
195
196       Playbooks should be written such that they can run from the command line: "ansible-playbook -i inventoryfile –extra-vars optionalvariables playbookname" That means the playbook should not contain any VM credentials information, they are expected to be provided through the inventory file passed at run time.
197
198         a.  Stand-alone playbooks
199
200         |image3|
201
202         b.  Playbooks in gzipped tarfiles
203
204         |image4|
205
206     d. Execution
207
208
209       Ansible docker image can be downloaded from like Nexus3_.  search the keyword: "ccsdk-ansible-server-image".
210
211 .. _Nexus3: https://nexus3.onap.org/ 
212
213       Also the dockerfile for ansible docker build is located at Dockerfile_.
214
215 .. _Dockerfile: https://gerrit.onap.org/r/gitweb?p=ccsdk/distribution.git;a=blob;f=ansible-server/src/main/Dockerfile;h=2751a48213e628f10631e251ed11e45c04f8466f;hb=HEAD
216
217       Ansible server is executed through RestServer.py. Its configuration file consists of the following:
218
219       .. code:: bash
220
221         # Host definition
222         ip: 0.0.0.0
223         port: 8000
224
225         # Security (controls use of TLS encrypton and RestServer authentication)
226         tls: no
227         auth: no
228
229         # TLS certificates (must be built on application host)
230         priv: provide\_privated\_key.pem
231         pub: provide\_public\_key.pem 
232
233         # RestServer authentication
234         id: provide\_RestServer\_id
235         psswd: provide\_password\_4\_RestServer\_id
236
237         # Mysql
238         host: localhost
239         user: mysql\_user\_id
240         passwd: password\_4\_mysql\_user\_id
241         db: ansible 
242
243         #Playbooks
244         from\_files: yes
245         ansible\_path: /home/ubuntu/RestServerOpenSource
246         ansible\_inv: Ansible\_inventory
247         ansible\_temp: PlaybooksTemp
248         timeout\_seconds: 60
249
250         # Blocking on GetResults
251         getresults\_block: yes
252
253 Execution and testing steps:
254
255  1.  **Start RestServer**: *python RestServer.py*
256
257    Note: RSA key fingerprint needs to be loaded manually in server for each VM defined in inventory file that requires ssh authentication. This can be done by testing ssh credentials to each target VM and accepting RSA key fingerprint:
258
259    .. code:: bash
260
261      ssh -i key \|VMaddress\|
262      RSA key fingerprint is \|something.\|
263      Are you sure you want to continue connecting (yes/no)? yes
264
265
266  2.  **Try curl commands** (case no secured REST: HTTP & no authentication)
267
268    Request to execute playbook:
269
270    .. code:: bash
271
272      curl -H "Content-type: application/json" -X POST -d '{"Id": "10", "PlaybookName": "ansible\_sleep", "NodeList": ["host"], "Timeout": "60", "EnvParameters": {"Sleep": "10"}}'http://0.0.0.0:8000/Dispatch
273
274    Response:
275
276    .. code:: bash
277
278      {"ExpectedDuration": "60sec", "StatusMessage": "PENDING", "StatusCode": 100}
279
280    Get results (blocked until test finished):
281
282    .. code:: bash
283
284      curl -H "Content-type: application/json" -X GET "http://0.0.0.0:8000/Dispatch/?Id=10&Type=GetResult"
285
286    Response:
287
288    .. code:: bash
289
290      {"Results": {"localhost": {"GroupName": "host", "StatusMessage": "SUCCESS", "StatusCode": 200}}, "PlaybookName":"ansible\_sleep", "Version": "0.00", "Duration": "11.261794", "StatusMessage": "FINISHED", "StatusCode": 200}
291
292    Delete playbook execution information
293
294    .. code:: bash
295
296      curl -H "Content-type: application/json" -X DELETE http://0.0.0.0:8000/Dispatch/?Id=10
297
298    Response:
299
300    .. code:: bash
301
302      {"StatusMessage": "PLAYBOOK EXECUTION RECORDS DELETED", "StatusCode": 200}
303
304 Playbook execution done through system call
305
306    .. code:: bash
307
308      ansible-playbook --v -extra-vars ‘playbookvars’ -i inventoryfile playbook.yml
309
310    - Inventory file created at run time, playbook loaded from mysql, both placed in the temporary directory destroyed at end of test (Playbook archive is unpacked in the temporary directory)
311
312 All tested playbooks written such that the ansible ‘play recap’ log indicates whether or not the playbook tasks succeeded (multiple tasks in a standalone playbook or playbooks with roles directory structure)
313
314 Sample ansible ‘play recap’:
315
316 |image6|
317
318
319
320 .. _Ansible: https://www.ansible.com/
321
322 .. |image0| image:: images/image0.png
323 .. |image1| image:: images/image1.png
324 .. |image2| image:: images/image2.png
325 .. |image3| image:: images/image3.png
326 .. |image4| image:: images/image4.png
327 .. |image5| image:: images/image5.png
328 .. |image6| image:: images/image6.png