Example #1
0
        /// <summary>
        /// Constructs an instance from the execution results of an
        /// <see cref="AnsiblePlayer"/> play operation.
        /// </summary>
        /// <param name="rawResults">The execution results.</param>
        internal AnsiblePlayResults(ExecuteResult rawResults)
        {
            Covenant.Requires <ArgumentNullException>(rawResults != null);

            RawResults = rawResults;

            if (rawResults.ExitCode != 0 && rawResults.ExitCode != 2)
            {
                // Must be a command line argument or playbook syntax error
                // as opposed to a valid playbook that had one or more failed
                // tasks.

                throw new Exception(rawResults.ErrorText);
            }

            using (var reader = new StringReader(rawResults.OutputText))
            {
                string line;

                // Skip over all lines until we see the first task line.

                for (line = reader.ReadLine(); line != null; line = reader.ReadLine())
                {
                    if (line.StartsWith("TASK [") && line.EndsWith("**********"))
                    {
                        break;
                    }
                }

                var sbTask   = new StringBuilder();
                var lastTask = false;

                while (!lastTask)
                {
                    // Capture the current line and any subsequent lines up to but not
                    // including the next task marker or the PLAY RECAP line and then
                    // use this to create the next task result.

                    sbTask.AppendLine(line);

                    for (line = reader.ReadLine(); line != null; line = reader.ReadLine())
                    {
                        if (line.StartsWith("TASK [") && line.EndsWith("**********"))
                        {
                            break;
                        }
                        else if (line.StartsWith("PLAY RECAP **********") && line.EndsWith("**********"))
                        {
                            lastTask = true;
                            break;
                        }

                        sbTask.AppendLine(line);
                    }

                    var taskResult = new AnsibleTaskResult(sbTask.ToString());

                    if (taskResult.HasStatus)
                    {
                        TaskResults.Add(taskResult);
                    }

                    if (!lastTask)
                    {
                        sbTask.Clear();
                        sbTask.AppendLine(line);    // This is the first line of the next task
                    }
                }
            }
        }