Lineinfile append at EOF

954 views
Skip to first unread message

Mona Gopal

unread,
Jun 14, 2016, 1:06:33 AM6/14/16
to Ansible Project
Hi all,

I am trying to use the lineinfile module to append  some of the software details to a file. 
I don't want to match to any regexp, but just insert every software details at the EOF.


Below is the piece of code.

- name: Collect python details
  action: get_python_details 
  register: python

- name: Write registered variable to file
  local_action: lineinfile dest={{ third_party_dest_path }}  line={{ python }} regexp="" insertafter=EOF state=present create=yes

Have also tried giving regexp="^$", regexp="^\s*$" as well to match to an empty line, but throws the below error.

Error:

An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1465888759.41-82910653497853/lineinfile", line 2540, in <module>
    main()
  File "/root/.ansible/tmp/ansible-tmp-1465888759.41-82910653497853/lineinfile", line 371, in main
    ins_aft, ins_bef, create, backup, backrefs)
  File "/root/.ansible/tmp/ansible-tmp-1465888759.41-82910653497853/lineinfile", line 266, in present
    lines.append(line + os.linesep)
TypeError: unsupported operand type(s) for +: 'dict' and 'str'

fatal: [172.19.3.60 -> localhost]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "lineinfile"}, "parsed": false}

Kindly help on how to go about appending to EOF without matching to any regexp.

Thanks in advance,
Mona G

Mona Gopal

unread,
Jun 14, 2016, 2:12:29 AM6/14/16
to Ansible Project
Hi All,

 I figured out that the below works to append to a file,

- name: Write redis details to file
  local_action: lineinfile dest={{ third_party_dest_path }} line=redis state=present create=yes

However, i am unable to append the registered variable value to the file

- name: Write redis details to file
  local_action: lineinfile dest={{ third_party_dest_path }} line="{{ redis }}" state=present create=yes

Jason Gilfoil

unread,
Jun 14, 2016, 10:07:56 AM6/14/16
to Ansible Project
Mona, I don't think you need the regexp parameter, just set 'insertafter: EOF' to get it to append at the end of the file. See for what works for me in one of my scripts:

- name: add script exec to bashrc
  lineinfile:
    dest: /home/.bashrc
    state: present
    line: '. /home/.profile-custom'
    insertafter: EOF

Abhishek R.

unread,
Jun 15, 2016, 8:01:47 AM6/15/16
to Ansible Project
You should probably try using {{ redis.stdout }}. Registered variables are not inherently strings, unlike extra_vars.

Regards
Reply all
Reply to author
Forward
0 new messages