Repository: ansible/ansible-lint

Issue:
#noqa does not work on pre_tasks and post_tasks
ansible-lint 4.1.0

Example playbook.yml:
```
---
- name: hello
  hosts: all
  pre_tasks:
    - name: Get Status # noqa 303 305
      shell: 'systemctl -l status'
      changed_when: false
  tasks:
    - name: Get Status # noqa 303 305
      shell: 'systemctl -l status'
      changed_when: false
  post_tasks:
    - name: Get Status # noqa 303 305
      shell: 'systemctl -l status'
      changed_when: false
```
Current output:
```
303] systemctl used in place of systemd module
playbook.yml:5
Task/Handler: Get Status

[305] Use shell only when shell functionality is required
playbook.yml:5
Task/Handler: Get Status

[303] systemctl used in place of systemd module
playbook.yml:13
Task/Handler: Get Status

[305] Use shell only when shell functionality is required
playbook.yml:13
Task/Handler: Get Status
```

Should not output anything ...


Maybe related to https://github.com/ansible/ansible-lint/issues/484


Discussion:

Resolving patch:
diff --git a/lib/ansiblelint/utils.py b/lib/ansiblelint/utils.py
--- a/lib/ansiblelint/utils.py
+++ b/lib/ansiblelint/utils.py
@@ -602,6 +602,14 @@ def append_skipped_rules(pyyaml_data, file_text, file_type):
     """ Uses ruamel.yaml to parse comments then adds a
         skipped_rules list to the task (or meta yaml block)
     """
+
+    PLAYBOOK_TASK_KEYWORDS = [
+        'tasks',
+        'pre_tasks',
+        'post_tasks',
+        'handlers',
+    ]
+
     yaml = ruamel.yaml.YAML()
     ruamel_data = yaml.load(file_text)
 
@@ -613,8 +621,9 @@ def append_skipped_rules(pyyaml_data, file_text, file_type):
             ruamel_tasks = []
             pyyaml_tasks = []
             for ruamel_play, pyyaml_play in zip(ruamel_data, pyyaml_data):
-                ruamel_tasks.extend(ruamel_play.get('tasks', []))
-                pyyaml_tasks.extend(pyyaml_play.get('tasks', []))
+                for key in PLAYBOOK_TASK_KEYWORDS:
+                    ruamel_tasks.extend(ruamel_play.get(key, []))
+                    pyyaml_tasks.extend(pyyaml_play.get(key, []))
         except (AttributeError, TypeError):
             return pyyaml_data
     elif file_type == 'meta':


Test patch:
diff --git a/test/TestSkipPlaybookItems.py b/test/TestSkipPlaybookItems.py
new file mode 100644
--- /dev/null
+++ b/test/TestSkipPlaybookItems.py
@@ -0,0 +1,88 @@
+import unittest
+import os
+
+from ansiblelint import RulesCollection
+from test import RunFromText
+
+
+PLAYBOOK_PRE_TASKS = '''
+- hosts: all
+  tasks:
+    - name: bad git 1  # noqa 401
+      action: git a=b c=d
+    - name: bad git 2
+      action: git a=b c=d
+  pre_tasks:
+    - name: bad git 3  # noqa 401
+      action: git a=b c=d
+    - name: bad git 4
+      action: git a=b c=d
+'''
+
+PLAYBOOK_POST_TASKS = '''
+- hosts: all
+  tasks:
+    - name: bad git 1  # noqa 401
+      action: git a=b c=d
+    - name: bad git 2
+      action: git a=b c=d
+  post_tasks:
+    - name: bad git 3  # noqa 401
+      action: git a=b c=d
+    - name: bad git 4
+      action: git a=b c=d
+'''
+
+PLAYBOOK_HANDLERS = '''
+- hosts: all
+  tasks:
+    - name: bad git 1  # noqa 401
+      action: git a=b c=d
+    - name: bad git 2
+      action: git a=b c=d
+  handlers:
+    - name: bad git 3  # noqa 401
+      action: git a=b c=d
+    - name: bad git 4
+      action: git a=b c=d
+'''
+
+PLAYBOOK_TWO_PLAYS = '''
+- hosts: all
+  tasks:
+    - name: bad git 1  # noqa 401
+      action: git a=b c=d
+    - name: bad git 2
+      action: git a=b c=d
+
+- hosts: all
+  tasks:
+    - name: bad git 3  # noqa 401
+      action: git a=b c=d
+    - name: bad git 4
+      action: git a=b c=d
+'''
+
+
+class TestSkipPlaybookItems(unittest.TestCase):
+    rulesdir = os.path.join('lib', 'ansiblelint', 'rules')
+    collection = RulesCollection.create_from_directory(rulesdir)
+
+    def setUp(self):
+        self.runner = RunFromText(self.collection)
+
+    def test_pre_tasks(self):
+        results = self.runner.run_playbook(PLAYBOOK_PRE_TASKS)
+        self.assertEqual(2, len(results))
+
+    def test_post_tasks(self):
+        results = self.runner.run_playbook(PLAYBOOK_POST_TASKS)
+        self.assertEqual(2, len(results))
+
+    def test_play_handlers(self):
+        results = self.runner.run_playbook(PLAYBOOK_HANDLERS)
+        self.assertEqual(2, len(results))
+
+    def test_two_plays(self):
+        results = self.runner.run_playbook(PLAYBOOK_TWO_PLAYS)
+        self.assertEqual(2, len(results))