{"action":"created","issue":{"url":"https://api.github.com/repos/ansible/ansible/issues/4531","repository_url":"https://api.github.com/repos/ansible/ansible","labels_url":"https://api.github.com/repos/ansible/ansible/issues/4531/labels{/name}","comments_url":"https://api.github.com/repos/ansible/ansible/issues/4531/comments","events_url":"https://api.github.com/repos/ansible/ansible/issues/4531/events","html_url":"https://github.com/ansible/ansible/issues/4531","id":21043944,"number":4531,"title":"Ansible lineinfile duplicates line","user":{"login":"zupo","id":311580,"avatar_url":"https://avatars.githubusercontent.com/u/311580?v=3","gravatar_id":"","url":"https://api.github.com/users/zupo","html_url":"https://github.com/zupo","followers_url":"https://api.github.com/users/zupo/followers","following_url":"https://api.github.com/users/zupo/following{/other_user}","gists_url":"https://api.github.com/users/zupo/gists{/gist_id}","starred_url":"https://api.github.com/users/zupo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zupo/subscriptions","organizations_url":"https://api.github.com/users/zupo/orgs","repos_url":"https://api.github.com/users/zupo/repos","events_url":"https://api.github.com/users/zupo/events{/privacy}","received_events_url":"https://api.github.com/users/zupo/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ansible/ansible/labels/bug_report","name":"bug_report","color":"fbca04"},{"url":"https://api.github.com/repos/ansible/ansible/labels/needs_info","name":"needs_info","color":"eb6420"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2013-10-15T20:56:29Z","updated_at":"2016-03-09T13:38:28Z","closed_at":"2013-10-15T21:03:32Z","body":"I have a simple file at /etc/foo.txt. The file contains the following:\r\n\r\n    #bar\r\n\r\nI have the following ansible playbook task to uncomment the line above:\r\n\r\n    - name: test lineinfile\r\n      lineinfile: backup=yes state=present dest=/etc/foo.txt\r\n                  regexp='^#bar'\r\n                  line='bar'\r\n\r\nWhen I first run ansible-playbook, the line gets uncommented and the /etc/foo.txt now contains the following:\r\n\r\n    bar\r\n\r\nHowever, if I run ansible-playbook again, I get the following:\r\n\r\n    bar\r\n    bar\r\n\r\nIf I run it yet again, then the /etc/foo.txt file will look like this:\r\n\r\n    bar\r\n    bar\r\n    bar\r\n\r\nI believe these duplications of lines are a bug.\r\n\r\nHappens on ansible-playbook 1.3.3 and 1.2.2."},"comment":{"url":"https://api.github.com/repos/ansible/ansible/issues/comments/194299306","html_url":"https://github.com/ansible/ansible/issues/4531#issuecomment-194299306","issue_url":"https://api.github.com/repos/ansible/ansible/issues/4531","id":194299306,"user":{"login":"beinbliss","id":7058053,"avatar_url":"https://avatars.githubusercontent.com/u/7058053?v=3","gravatar_id":"","url":"https://api.github.com/users/beinbliss","html_url":"https://github.com/beinbliss","followers_url":"https://api.github.com/users/beinbliss/followers","following_url":"https://api.github.com/users/beinbliss/following{/other_user}","gists_url":"https://api.github.com/users/beinbliss/gists{/gist_id}","starred_url":"https://api.github.com/users/beinbliss/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beinbliss/subscriptions","organizations_url":"https://api.github.com/users/beinbliss/orgs","repos_url":"https://api.github.com/users/beinbliss/repos","events_url":"https://api.github.com/users/beinbliss/events{/privacy}","received_events_url":"https://api.github.com/users/beinbliss/received_events","type":"User","site_admin":false},"created_at":"2016-03-09T13:38:28Z","updated_at":"2016-03-09T13:38:28Z","body":"^This. It should not add lines if regex does not match(?).\r\n\r\nFor example, if there is a string in file \"weekly\" and the first run changes it to \"daily\", the next run will add \"daily\" line to the bottom of the file. Example:\r\n lineinfile:\r\n    dest: /etc/logrotate.conf\r\n    regexp: \"{{ item.regexp }}\"\r\n    line: \"{{ item.line }}\"\r\n  with_items:\r\n    - { regexp: '^#?weekly', line: 'daily' }\r\n"}}