Issue 1:
ISSUE TYPE

Bug Report

COMPONENT NAME

lineinfile

ANSIBLE VERSION

[See issue #16596](https://github.com/ansible/ansible/issues/16596)

CONFIGURATION

OS / ENVIRONMENT

SUMMARY

This bug still exists in version 2.2. The original bug report was closed in July 2016 due to the old module bug reporting policy that bugs for modules are reported at https://github.com/ansible/ansible-modules-core. That repository is now closed for bug reports as of Dec 2016 and requests bugs to be submitted to this repository. (See the last comment on this bug report for an example). Additionally, a response to issue #16596 that the bug was already reported is incorrect. Issue #16596 deals with quantifiers in regular expressions not working as advertised. The supposedly duplicate bug report deals with lineinfile adding a line when the regex is not found. Closing issue reporting for a repo without also migrating the open issues to the new reporting site does not make those issues go away.

Also, see issue #16596

STEPS TO REPRODUCE

See issue #16596

[See issue #16596](https://github.com/ansible/ansible/issues/16596)

EXPECTED RESULTS

See issue #16596

ACTUAL RESULTS

[See issue #16596](https://github.com/ansible/ansible/issues/16596)

Issue 2:
From @jsnshrmn on 2016-06-17T15:03:20Z

ISSUE TYPE

Bug Report

COMPONENT NAME

lineinfile

ANSIBLE VERSION

ansible 2.1.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides

CONFIGURATION

Bone stock

OS / ENVIRONMENT

CentOS 7

SUMMARY

When lineinfile with regexp finds a match, it substitutes properly. When it doesn't find a match (say, on subsequent runs), it just dumps the specified line at the bottom of the file as if you hadn't specified a regexp. Needless to say, this breaks idempotence.

STEPS TO REPRODUCE

Take a look at a file.

Run lineinfile with regexp that matches a line

See that your line was in fact replaced.

Run lineinfile again.

See that the specified replacement line is now duplicated at the bottom of the file.

- name: Add ops scripts to sudo secure_path
lineinfile:
dest: /etc/sudoers
regexp: >
^Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin$
line: 'Defaults secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin'
validate: visudo -cf %s

EXPECTED RESULTS

Idempotent line replacement after a second run

# Refuse to run if unable to disable echo on the tty.
Defaults !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults always_set_home

Defaults env_reset
Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults env_keep += "HOME"

Defaults secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL

## Allows people in group wheel to run all commands without a password
%wheel ALL=(ALL) NOPASSWD: ALL

ACTUAL RESULTS

Potentially show-stopping garbage at the bottom of a file

# Refuse to run if unable to disable echo on the tty.
Defaults !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults always_set_home

Defaults env_reset
Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults env_keep += "HOME"

Defaults secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL

## Allows people in group wheel to run all commands without a password
%wheel ALL=(ALL) NOPASSWD: ALL
Defaults secure_path = /opt/d7/bin:/sbin:/bin:/usr/sbin:/usr/bin

Copied from original issue: ansible/ansible-modules-core#3975