Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot parse YAML file, (Line: 2, Col: 3, Idx: 6) - (Line: 2, Col: 3, Idx: 6): Exception during deserialization #820

Closed
rudolfolah opened this issue Jun 19, 2023 · 2 comments

Comments

@rudolfolah
Copy link
Contributor

Describe the bug

I'm encountering an issue while trying to load an Ansible task file, where I receive an exception related to deserialization.

When attempting to load the Ansible task file, the following exception arises:

Unhandled exception. (Line: 2, Col: 3, Idx: 6) - (Line: 2, Col: 3, Idx: 6): Exception during deserialization

To Reproduce

Here is the specific C# code I am executing:

string tasksMainYaml = File.ReadAllText("../../../sample_data/tasks_main.yml");
var yamlDeserializer = new DeserializerBuilder().Build();
var parser = new Parser(new StringReader(tasksMainYaml));
YamlDotNet.RepresentationModel.YamlStream yaml = new YamlDotNet.RepresentationModel.YamlStream();
yaml.Load(new StringReader(tasksMainYaml));
parser.Expect<StreamStart>();

while (parser.Accept<DocumentStart>())
{
    // Deserialize the document
    var doc = yamlDeserializer.Deserialize<List<string>>(parser);

    Console.WriteLine("## Document");
    foreach (var item in doc)
    {
        Console.WriteLine(item);
    }
}

The YAML file that I am trying to parse includes the following:

---
- name: Install certbot
  tags: setup
  community.general.snap:
    name: certbot
    classic: true
- name: Create symlink for Certbot binary
  tags: setup
  ansible.builtin.file:
    src: /snap/bin/certbot
    dest: /usr/bin/certbot
    state: link
  become: true
- name: Check if the certificate was already generated
  tags: generate_cert
  ansible.builtin.stat:
    path: "/etc/letsencrypt/live/{{ domain }}/fullchain.pem"
  register: cert_fullchain_file
- name: Generate certificate manually
  tags: generate_cert
  ansible.builtin.command:
    cmd: "certbot certonly --standalone --preferred-challenges http --agree-tos --email {{ email }} -d {{ domain }}"
    creates: "/etc/letsencrypt/live/{{ domain }}/fullchain.pem"
  when: not cert_fullchain_file.stat.exists

Any assistance in troubleshooting this issue would be greatly appreciated.

@EdwardCooke
Copy link
Collaborator

You're trying to deserialize a mapping into a string which isn't going to work. If instead you did this in your while loop:

var doc = yamlDeserializer.Deserialize<List<object>>(parser);
Console.WriteLine("## Document");
foreach (Dictionary<object, object> item in doc)
{
    foreach (var kvp in item)
    {
        Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
    }
    Console.WriteLine("======");
}

It will result in:

## Document
name: Install certbot
tags: setup
community.general.snap: System.Collections.Generic.Dictionary`2[System.Object,System.Object]
======
name: Create symlink for Certbot binary
tags: setup
ansible.builtin.file: System.Collections.Generic.Dictionary`2[System.Object,System.Object]
become: true
======
name: Check if the certificate was already generated
tags: generate_cert
ansible.builtin.stat: System.Collections.Generic.Dictionary`2[System.Object,System.Object]
register: cert_fullchain_file
======
name: Generate certificate manually
tags: generate_cert
ansible.builtin.command: System.Collections.Generic.Dictionary`2[System.Object,System.Object]
when: not cert_fullchain_file.stat.exists
======

Since you're not specifying a class you'll need to reference everything as the expected datatype in the yaml, either string, List<object> or Dictionary<object, object>. Where object will generally be one of string, List<object>, or Dictionary<object, object>.

@rudolfolah
Copy link
Contributor Author

amazing, that worked! thank you for the explanation. I've created a PR that updates the error message and adds a sample based on your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants