0

What I got as extra variable to ansible playbook is "CHOW_app/timmy_app1/johnn_app3/harper_app4/mona_app5". This is passed as single variable to the playbook. I have to separate it and save it in a text file in this format.

REVOKE CHOW app
REVOKE timmy app1
REVOKE johnn app3
REVOKE harper app4 
REVOKE mona app5

I think it can be achievable using awk command. But I dont know whether this can be done in Ansible playbook itself. Does anyone have any idea how we can separate it using awk and store it in a txt file in Ansible itself.

1
  - name: slash delimited template
    template:
      src: revoke.jinja
      # dest is the output file
      dest: /tmp/revoke
    vars:
      # "variable" is the input delimited var
      # Split it into a list of users
      revokes: "{{ variable.split('/') }}"

templates/revoke.jinja file contains

{% for user in revokes %}
REVOKE {{ user.split('_') | join(' ') }}
{% endfor %}
3
  • This is giving me result. But it is like this: "REVOKE CHOW appREVOKE timmy app1REVOKE johnn app3REVOKE harper app4REVOKE mona app5" . Any idea how to get next REVOKE to new line. here it is appending to "app" word without going to new line instead. I added " newline_sequence: '\n' " to the template module. But still it gave the same result – saffron May 22 '20 at 14:18
  • When testing on Linux, I got line feed endings (\n). Try adding whatever new line endings you need inside the for loop. – John Mahowald May 22 '20 at 14:47
  • Yes. In Linux, I am getting the expected result. But I am creating this txt file in windows. There it is not appending to new line. – saffron May 23 '20 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.