---
- name: "Add the agent to the {{ dynatrace_java_agent_env_var_name }} environment variable definition in file {{ dynatrace_java_agent_env_var_file_name }}"
  lineinfile:
    dest: "{{ dynatrace_java_agent_env_var_file_name }}"
    regexp: 'export {{ dynatrace_java_agent_env_var_name }}="\${{ dynatrace_java_agent_env_var_name }} {{ dynatrace_java_agent_agentpath_string }}"'
    line: 'export {{ dynatrace_java_agent_env_var_name }}="${{ dynatrace_java_agent_env_var_name }} {{ dynatrace_java_agent_agentpath_string }}"'
    insertafter: "{{ dynatrace_java_agent_env_var_file_insert_after }}"
    state: "{{ dynatrace_java_agent_state }}"
    create: yes
    backup: yes
  sudo: yes

---
- name: "Add the agent to the {{ dynatrace_java_agent_env_var_name }} environment variable definition in file {{ dynatrace_java_agent_env_var_file_name }}"
  lineinfile:
    dest: "{{ dynatrace_java_agent_env_var_file_name }}"
    regexp: 'export {{ dynatrace_java_agent_env_var_name }}="\${{ dynatrace_java_agent_env_var_name }} {{ dynatrace_java_agent_agentpath_string }}"'
    line: 'export {{ dynatrace_java_agent_env_var_name }}="${{ dynatrace_java_agent_env_var_name }} {{ dynatrace_java_agent_agentpath_string }}"'
    insertafter: "{{ dynatrace_java_agent_env_var_file_insert_after }}"
    state: "{{ dynatrace_java_agent_state }}"
    create: yes
    backup: yes
  become: yes

Use 'become' instead of 'sudo'.
Use 'become' instead of 'sudo'.
