replace module query

Hi All,

I have one query regarding ansible replace module, I wrote one ansible playbook and I used replace module in my ansible playbook, it is running successfully but without making any changes in file,

  • replace:
    path: /home/xxx/known_host.txt
    regexp: ‘awk “/[1]*($|#)/{next} /$host_to_find/{print $2 " " }”’
    replace: ‘\Sumit’
    backup: yes

It is running fine, but without making any changes in the file.

Looking forward for reply.

Thanks & Regards
Sumit Sahay


  1. [:space:] ↩︎

Hi

What is the question that you would like to get a reply to?

Also, what do you mean by "it is running fine"? If you want to replace
something, but it's not making that change, to me that would qualify
as "it does not work".

And finally - the regex pattern seems to indicate that you are trying
to replace an awk script in a file with just the string "\Sumit" - are
you sure that is your intention?

Perhaps instead try to describe your goal (I would like to replace
occource x/y with z, based on A, b, etc).

Thanks for clarifying

Dick

Hi Dick Visser,
Thanks for your reply.
Yeah I want to replace the output of awk with string.
The playbook is running fine it means it is getting executed without any error, that is what I was trying to say.

Actually the string may vary as per the requirement as of now I used my name to raise my question on Ansible forum.

Looking forward for your reply.

Regards
Sumit Sahay

Hi Sumit

For clarity sake, the 'replace' module "will replace all instances of
a pattern within a file" (as per
https://docs.ansible.com/ansible/latest/modules/replace_module.html).
There is no error because there is nothing to replace.
However, if you mention "running awk first", then you should, well,
run awk first. But I don't see that anywhere.
If you intend to do that, you should use either the 'shell' or the
'command' module first.
Possibly just use a regular expression filter in that same task (and
avoid having two tasks that each need a file to work on).
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#regular-expression-filters

Things would be a lot easier if you were able to provide a real data
sample, and a clear description of what you're exactly trying to
achieve.

Thanks

Dick

Hello Dick, thank you for your response.
I started studying Ansible this week and I’m already using on some servers with Ad-Hoc commands.
I plan to make some shell scripts and include them in some Playbooks.
While planning the creation of these scripts I raised my doubt.
However, the answers I received here were a great help and I will continue the project, and without doubt if more doubts arise during a real case I will bring it here again.
Thanks to all who responded so quickly and congratulations to the Ansible team for this project.
I’m really excited and happy to have met this powerful tool!

Hi Dick Visser,

Thanks for your reply.
As per your suggestion I am trying to running the shell module first and registering the output, now what should I do and how I used this register values in replace module to get the required output.

  • shell: ‘awk “/[1]*($|#)/{next} /$host_to_find/{print $1 " " }”’
    register: extracted_data

For reference I am attaching the file which I am using for this playbook.

xx.yy.zz.aa ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEA6TCzDMKmc/xnTB7Eb /7xB1ClCYSSA3CbbxaZd31KHV2dI5YdhFzhyMvpilVn5/lBEWk2fOTdRCoieEgmo7gpZl8rQGiqbw7+ePIUGO7JUD5nU+mkRDZTTjupPk/qdjz7gNTSLWDvcbby9QLB3SHHE5iWBgAK1y6yErDgGSsaGV1r7d7XP/Ni0koMJvtZ8ZjC1SP8Ao+rfa+62jjgHmlDxuMqny1OhtT2y12hHQOJC9Vk27IAPdlilouvw7h1sIF2Lt5xr5XQvIJqDcdJXUM+T1a1sessCRq2SQiu7WZc6ID9GPLy2pso552vX7ATSPuSwFtPU/6dZPmgfggFoD7IIw==
xx.yy.zz.aa ssh-rsaAAAAB3NzaC1yc2EAAAADAQABAAABAQDcUrYa1 Gjyf6PNGYLiorFNFsUiEj6NakUkfzMUEX2UJgA+ltt4mdbbnkryrrRulxQDQysAeZQRiuvobp7HNI9bNtdXZU8VsNTXp9etpxmbfVPXMD8FCEOnZSxb1BjoQ7w6w+/pqtGw3i1MUlMIreJWYW87J2CpbiyYufhh74HljLSjfMy1SZcUIFYKV2iJDB3H9GeWhdTT2vkmF9Tx9PMpt4miwKOwVnFs5rrki1nNQbvyesfd3yzTMa3gE0iuIVw3NWHiNN8x65QtfeVlKiXKnGjJO0FUXdilSogz5Aghqswfst7begxyDmvPiz9ALKhp90OBZTI1UwDAMzLTemF5
xx.yy.zz.aa ssh-rsaAAAAB3NzaC1yc2EAAAADAQABAAABAQCrs1Nk7xV2rJ k02oCSdV3UnT3eKSliJ06BbxeigLIIUC8P5cKQsxEUwG/DP4G06uzr6xIW31LSUDy/lPMZZtPJIunE6fBSu0CWcw0BPHixjNl0ujbuwfHBAfZYyN89bd3KZ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P44QDu72srf7vEej0eG1HgCrFKE0mWYyoiYz7ajjslAtvv5GngKrpzkynArriiyxkfeqM8j/G3ZHFn9ZT43k0zxrxq6Po9BvDfwJQwSt2TBq1GPu+jgfNp0s11Uqw7LYSybTxZtmvAOjvzs6juXpn/GFRgUm2ND9qc2sd/Eo4v2WkJiG9fMApxs7iluZ5M91T+8OtSQNEnTzqlnFMcEWAwWhBxy7oOiROeWFmdF6MnbXL2R0hLhdppNXseqA0f+yteVoLlF39X3P2ztDb2VEeyPmgfaFyO7qRb/NH1db3+iQ==

It is a known host file which I am using for my playbook.

Now, how do I pass this register value in replace module or how do I replace the value in file with the new value.

Looking forward for your reply.

Regards
Sumit Sahay


  1. [:space:] ↩︎