How to create a puppet module with pdk using ansible?

Hi,

I’m trying to create a new puppet module using pdk (pdk new module moduleName) with ansible, but this command requires some prompts and I’m struggling to make it work.

I tried the ansible expect module, but without success.

Here’s the command from shell:

[fabiosakiyama@puppetmaster modules]$ pdk new module conf
pdk (INFO): Creating new module: conf

We need to create the metadata.json file for this module, so we're going to ask you 4 questions.
If the question is not applicable to this module, accept the default option shown after each question. You can modify any answers at any time by manually updating the metadata.json file.

[Q 1/4] If you have a Puppet Forge username, add it here.
We can use this to upload your module to the Forge when it's complete.
--> fabiosakiyama

[Q 2/4] Who wrote this module?
This is used to credit the module's author.
--> fabiosakiyama

[Q 3/4] What license does this module code fall under?
This should be an identifier from https://spdx.org/licenses/. Common values are "Apache-2.0", "MIT", or "proprietary".
--> Apache-2.0

[Q 4/4] What operating systems does this module support?
Use the up and down keys to move between the choices, space to select and enter to continue.
--> RedHat based Linux, Debian based Linux, Windows

Metadata will be generated based on this information, continue? Yes

Here’s the playbook task:

- name: Create conf module
  expect:
    chdir: /etc/puppetlabs/code/environments/production/modules/
    command: /opt/puppetlabs/pdk/bin/pdk new module conf
    responses:
      .*: '\r'
      .*: '\r'
      .*: '\r'
      .*: '\r'
      .*: '\r'

I want to answer 'enter' to all answers, since there's a default answer. So i tried with '\r' and ' '.

Any help?

Thanks in advance!

Hi,

I’m not sure about how to do what you want with the expect module, but if all you want to do is to create a new module with the default configurations, you can do so adding the --skip-interview flag to pdk new module (at least according to the documentation at https://puppet.com/docs/pdk/1.x/pdk_creating_modules.html#concept-9353, I personally haven’t tried it yet):

"Optionally, to omit the interview questions and create the module with default metadata values, add the skip-interview flag: pdk new module <MODULE_NAME> --skip-interview"

The question/prompt part of responses must be unique, if you have more than one response for a prompt you need to make them a list under the same prompt.

.* doesn't give meaning in expect, "." is any character and "*" is zero or more, so this will trigger a response way before any prompt is printed.

This should work since there are a little delay before expect write the response:

     responses:
       '-->':
         - ''
       continue\?: ''

Hi Guilherme! Just tested and it worked! Thanks a lot!

But now I’m curious how to make it work without skipping the interview.

Hi Kai,

Thanks for clarifying!

Took some time to understand your answer, could you confirm if I’m correct?

When I write:

- name: Create conf module
  expect:
    chdir: /etc/puppetlabs/code/environments/production/modules/
    command: /opt/puppetlabs/pdk/bin/pdk new module conf
    responses:
      '-->':
        - " 
        - "
        - " 
        - "
      continue\?: "

I'm telling that I have 2 pattern for the questions:
- The questions that ends with -->
- The question that ends with 'continue?'
And the " means that the answer is just an enter. Is that correct?

`

Unfortunately, the code above didn’t work. The error is:

ERROR! Syntax Error while loading YAML.
found unexpected end of stream

The error appears to have been in ‘/etc/ansible/roles/puppetserver/tasks/main.yml’: line 37, column 1, but may
be elsewhere in the file depending on the exact syntax problem.

(specified line no longer in file, maybe it changed?)

`


When I deleted the last line ('continue\?'), it didn't fail because of the syntax, instead it failed because:

fatal: [puppetmaster]: FAILED! => {"changed": false, "msg": "No remaining responses for '-->', output was ' \u001b[36m(fabiosakiyama)\u001b[0m \u001b[2K\u001b[1G-->'"}

Based on that, it seems that he had the answers for the first 4 questions, but not for the last question. 

So I assumed I'm missing something on the syntax to code the last question/answer.

I tried as well like this and some other variations:
    responses:
      '-->':
        - " 
        - "
        - " 
        - "
      'continue?': "


Any clues?

Thanks in advance!

Hi Kai,

Thanks for clarifying!

Took some time to understand your answer, could you confirm if I'm correct?

When I write:

- name: Create conf module
  expect:
    chdir: /etc/puppetlabs/code/environments/production/modules/
    command: /opt/puppetlabs/pdk/bin/pdk new module conf
    responses:
      '-->':
        - "
      continue\?: "

I'm telling that I have 2 pattern for the questions:

- The questions that ends with -->

- The question that ends with 'continue?'

That is correct.
The question mark is escaped since ? is a special character in regexp.

And the " means that the answer is just an enter. Is that correct?

It should be two single quote or two double quotes, not one double quote as you have here.
A empty string will be the same as just pressing enter key.

Unfortunately, the code above didn't work. The error is:

ERROR! Syntax Error while loading YAML.
found unexpected end of stream

The error appears to have been in '/etc/ansible/roles/puppetserver/tasks/
main.yml': line 37, column 1, but may
be elsewhere in the file depending on the exact syntax problem.

(specified line no longer in file, maybe it changed?)

When I deleted the last line ('continue\?'), it didn't fail because of
the syntax, instead it failed because:

fatal: [puppetmaster]: FAILED! => {"changed": false, "msg": "No
remaining responses for '-->', output was '
\u001b[36m(fabiosakiyama)\u001b[0m \u001b[2K\u001b[1G-->'"}
Based on that, it seems that he had the answers for the first 4
questions, but not for the last question.

So I assumed I'm missing something on the syntax to code the last
question/answer.

I tried as well like this and some other variations:

    responses:
      '-->':
        - "
      'continue?': "

Any clues?

I think all of it is because of the missing quotes.