27

I wrote a playbook which installs and configures Google Authenticator per user.

I want the last step of the playbook to cat the google_authenticator configuration file.

Using the "debug" module I am able to get the data to be displayed on screen but only as debug message:

TASK: [debug var=details.stdout_lines] ****************************************
ok: [localhost] => {
    "details.stdout_lines": [
        "ZKMFTE2ADYA2OYCH",
        "\"RATE_LIMIT 3 30",
        "\" DISALLOW_REUSE",
        "\" TOTP_AUTH",
        "12920994",
        "88224784",
        "69464205",
        "38144121",
        "45634120"
    ]
}

I read online that I can do something like that:

  - name: Print to screen google authenticator details
    command: /bin/cat {{ google_authenticator_secret_file_location }}
    register: details
    tags: google_2fa_user

  - debug: msg="{{ details.stdout_lines }}"

But I get an error when I run it:

TASK: [Print to screen google authenticator details] **************************
changed: [localhost]

TASK: [debug msg="{{details.stdout_lines}}"] **********************************
fatal: [localhost] => Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 532, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 629, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 815, in _executor_internal_inner
    result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/action_plugins/debug.py", line 41, in run
    kv = utils.parse_kv(module_args)
  File "/usr/lib/python2.7/dist-packages/ansible/utils/__init__.py", line 526, in parse_kv
    vargs = [x.decode('utf-8') for x in shlex.split(args, posix=True)]
  File "/usr/lib/python2.7/shlex.py", line 279, in split
    return list(lex)
  File "/usr/lib/python2.7/shlex.py", line 269, in next
    token = self.get_token()
  File "/usr/lib/python2.7/shlex.py", line 96, in get_token
    raw = self.read_token()
  File "/usr/lib/python2.7/shlex.py", line 172, in read_token
    raise ValueError, "No closing quotation"
ValueError: No closing quotation


FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************

The error says: "No closing quotation" although it is quoted. Also tried:

 - debug: msg= "{{ details.stdout_lines }}"

Any idea what could be the problem?

4 Answers 4

6

The quote Jinja filter should solve the quoting problem. Use it like this:

  - debug: msg="{{ details.stdout_lines | quote }}"

For the other question, I am not aware of a module to print statements other than the debug module. You might want to check if Save registered variable to file is an option. If you want to store Ansible variables on the controller host it is possible to do something like this:

- local_action: copy content={{ details.stdout_lines }} dest=/path/to/destination/file

EDIT I need to correct myself a bit. Take a look at this serverfault question. You can tweak the Ansible output by using the callback.display function. I recommend reading the linked blog post.

1

I would bet that the problem is the quotes in the file you are cat'ing are mismatched and messing with the quotes in the msg. Maybe try:

- debug: msg="{{ details.stdout_lines | regex_escape() }"

or

- debug: msg="{{ details.stdout_lines | regex_replace('"', '\"') }"

This should escape the quotes in the msg so that the quotes around the msg will match each other.

This has not been tested (I'm not in a position to test it right now), but you could try it real quick and see.

2
  • Tried both... didn't work.
    – Itai Ganot
    Jun 2, 2016 at 8:13
  • Hmm, I just ran it without my suggestions above, and got the same output you had at the beginning. However it doesn't appear you can get it in a format other than debug, without writing your own log module for ansible, or piping to a shell or perl script or something. This link had a good answer stackoverflow.com/questions/28564811/…
    – lsd
    Jun 2, 2016 at 16:36
1

I've looked deeply through the internet and checked with some Ansible professionals.

As far as I understand, there's no such option in Ansible 1.8 to redirect the output of the command to the screen as normal output rather than debug output.

1
  • 2
    As to Ansible 2.2 there's still no option to print to screen other than using debug.
    – Itai Ganot
    Apr 13, 2017 at 6:41
0

I ran some tests on the block of text you had above– dropped it into place and cleaned off the json quotes added in by using details.stdout_lines.

If the 'bad' text in your auth file is always a leading \", then this (tested) just happens to work, producing much the same output but with a colon in place of this one string.

- debug: msg="{{ details.stdout.replace('\\"',':').split('\n') }}"

Now this is an extremely limited use-case, but if the google auth output is strictly defined here (and it's entirely possible that's the case), then this should do what you want.

However, it would still be easier, and preferable to use var=details.stdout_lines to just get the contents here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .