How can I execute task only when file not contain a string?

1,617 views
Skip to first unread message

Stéphane Klein

unread,
Feb 24, 2017, 2:40:43 PM2/24/17
to Ansible Project
Hi,

is there a solution more user friendly that http://stackoverflow.com/questions/30786263/only-check-whether-a-line-present-in-a-file-ansible to test if file contain a string?

I don't like that:

- name: Check if bash-completion in enable for root user
  command: grep -q "/etc/bash_completion" /root/.bashrc
  register: check_bashcompletion_enabled
  ignore_errors: True

- name: Enable bach-completion for root user
  command: cp /etc/skel/.bashrc /root/.bashrc
  when: check_bashcompletion_enabled.rc == 1

Do you know a better solution?

Best regards,
Stéphane

Kai Stian Olstad

unread,
Feb 24, 2017, 3:26:38 PM2/24/17
to ansible...@googlegroups.com
What I would do is put the correct .bashrc in Ansible and just use the
copy module to copy it to the host(s).

--
Kai Stian Olstad

Stéphane Klein

unread,
Feb 24, 2017, 3:56:32 PM2/24/17
to Ansible Project, ansible-pr...@olstad.com


Le vendredi 24 février 2017 21:26:38 UTC+1, Kai Stian Olstad a écrit :
> Do you know a better solution?

What I would do is put the correct .bashrc in Ansible and just use the
copy module to copy it to the host(s).


I would like copy only if bash_completion isn't already present in .bashrc

Kai Stian Olstad

unread,
Feb 24, 2017, 4:40:05 PM2/24/17
to ansible...@googlegroups.com
Then you don't have much choice but go for the solution you posted.

But to make the output nicer I would replace "ignore_errors: True"
with

failed_when: False

or

failed_when: check_bashcompletion_enabled.rc > 1
changed_when: check_bashcompletion_enabled.rc == 1


What is it about your solution you don't like?

--
Kai Stian Olstad

Stéphane Klein

unread,
Feb 26, 2017, 8:36:33 AM2/26/17
to Ansible Project, ansible-pr...@olstad.com

Yes, thank it's better.

Matt Martz

unread,
Feb 26, 2017, 10:50:44 AM2/26/17
to ansible...@googlegroups.com, ansible-pr...@olstad.com
My preference in how to achieve this in a more ansible way, is using slurp, and then checking the returned contents:

- name: Get /root/.bashrc
  slurp:
    src:  /root/.bashrc
  register: root_bashrc

- name: Enable bash-completion for root user
  copy:
    src: /etc/skel/.bashrc
    dest: /root/.bashrc
    remote_src: true
  when: "'/etc/bash_completion' not in root_bashrc.content|b64decode"

I also replaced your command to copy the file, with the copy module, using remote_src=true

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/69f54eab-e80d-4626-95b8-c3187f324428%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Brian Coca

unread,
Feb 27, 2017, 5:18:41 PM2/27/17
to Ansible Project, ansible-pr...@olstad.com
it might be easier with the find module

- find: paths=... contains='whatever'
register: found

- command: run if found
when: found.matched > 0


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages