How to Separate Variables Using AWK and Append to Text File with Ansible

ansibleansible-playbookawkperlshell

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.

Best Answer

  - 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 %}