From 15b6837daf08f8baa94c4fea7935eb02d94fe9d5 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 23 May 2018 05:49:30 -0700 Subject: [PATCH] Add yamllint for plugin docs and fix issues. --- lib/ansible/plugins/inventory/constructed.py | 2 +- lib/ansible/plugins/lookup/chef_databag.py | 4 ++-- lib/ansible/plugins/lookup/config.py | 3 ++- lib/ansible/plugins/lookup/conjur_variable.py | 4 ++-- lib/ansible/plugins/lookup/dict.py | 12 ++++++------ lib/ansible/plugins/lookup/items.py | 3 ++- lib/ansible/plugins/lookup/nested.py | 2 +- lib/ansible/plugins/lookup/nios.py | 2 +- lib/ansible/plugins/lookup/password.py | 2 +- lib/ansible/plugins/lookup/vars.py | 2 +- test/runner/lib/sanity/yamllint.py | 4 ++++ test/sanity/yamllint/config/plugins.yml | 19 +++++++++++++++++++ test/sanity/yamllint/yamllinter.py | 10 ++++++++-- 13 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 test/sanity/yamllint/config/plugins.yml diff --git a/lib/ansible/plugins/inventory/constructed.py b/lib/ansible/plugins/inventory/constructed.py index 3fe98486812..39b9f37ba39 100644 --- a/lib/ansible/plugins/inventory/constructed.py +++ b/lib/ansible/plugins/inventory/constructed.py @@ -19,7 +19,7 @@ DOCUMENTATION = ''' - constructed ''' -EXAMPLES = ''' +EXAMPLES = r''' # inventory.config file in YAML format plugin: constructed strict: False diff --git a/lib/ansible/plugins/lookup/chef_databag.py b/lib/ansible/plugins/lookup/chef_databag.py index 2941926792b..08e132e64eb 100644 --- a/lib/ansible/plugins/lookup/chef_databag.py +++ b/lib/ansible/plugins/lookup/chef_databag.py @@ -28,8 +28,8 @@ DOCUMENTATION = """ """ EXAMPLES = """ - - debug - msg: {{ lookup('chef_databag', 'name=data_bag_name item=data_bag_item') }} + - debug: + msg: "{{ lookup('chef_databag', 'name=data_bag_name item=data_bag_item') }}" """ RETURN = """ diff --git a/lib/ansible/plugins/lookup/config.py b/lib/ansible/plugins/lookup/config.py index 53bb2f7f076..20ec25427fc 100644 --- a/lib/ansible/plugins/lookup/config.py +++ b/lib/ansible/plugins/lookup/config.py @@ -31,7 +31,8 @@ EXAMPLES = """ debug: msg="{{ lookup('config', 'DEFAULT_BECOME_USER')}}" - name: print out role paths - debug: msg="These are the configured role paths: {{lookup('config', 'DEFAULT_ROLES_PATH')}}" + debug: + msg: "These are the configured role paths: {{lookup('config', 'DEFAULT_ROLES_PATH')}}" - name: find retry files, skip if missing that key find: diff --git a/lib/ansible/plugins/lookup/conjur_variable.py b/lib/ansible/plugins/lookup/conjur_variable.py index 02931a801a2..20e6a704249 100644 --- a/lib/ansible/plugins/lookup/conjur_variable.py +++ b/lib/ansible/plugins/lookup/conjur_variable.py @@ -43,8 +43,8 @@ DOCUMENTATION = """ """ EXAMPLES = """ - - debug - msg: {{ lookup('conjur_variable', '/path/to/secret') }} + - debug: + msg: "{{ lookup('conjur_variable', '/path/to/secret') }}" """ RETURN = """ diff --git a/lib/ansible/plugins/lookup/dict.py b/lib/ansible/plugins/lookup/dict.py index 459235fad93..36e572e2246 100644 --- a/lib/ansible/plugins/lookup/dict.py +++ b/lib/ansible/plugins/lookup/dict.py @@ -19,12 +19,6 @@ DOCUMENTATION = """ """ EXAMPLES = """ -tasks: - - name: show dictionary - debug: msg="{{item.key}}: {{item.value}}" - with_dict: {a: 1, b: 2, c: 3} - -# with predefined vars vars: users: alice: @@ -34,10 +28,16 @@ vars: name: Bob Bananarama telephone: 987-654-3210 tasks: + # with predefined vars - name: Print phone records debug: msg: "User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})" loop: "{{ lookup('dict', users) }}" + # with inline dictionary + - name: show dictionary + debug: + msg: "{{item.key}}: {{item.value}}" + with_dict: {a: 1, b: 2, c: 3} """ RETURN = """ diff --git a/lib/ansible/plugins/lookup/items.py b/lib/ansible/plugins/lookup/items.py index 7c757e4d23e..80ec60dce0b 100644 --- a/lib/ansible/plugins/lookup/items.py +++ b/lib/ansible/plugins/lookup/items.py @@ -40,7 +40,8 @@ EXAMPLES = """ - testuser2 - name: "loop through list from a variable" - debug: msg="An item: {{item}}" + debug: + msg: "An item: {{item}}" with_items: "{{ somelist }}" - name: more complex items to add several users diff --git a/lib/ansible/plugins/lookup/nested.py b/lib/ansible/plugins/lookup/nested.py index ae402aa5e65..45304b7be5a 100644 --- a/lib/ansible/plugins/lookup/nested.py +++ b/lib/ansible/plugins/lookup/nested.py @@ -27,7 +27,7 @@ EXAMPLES = """ with_nested: - [ 'alice', 'bob' ] - [ 'clientdb', 'employeedb', 'providerdb' ] -As with the case of 'with_items' above, you can use previously defined variables.: +# As with the case of 'with_items' above, you can use previously defined variables.: - name: here, 'users' contains the above list of employees mysql_user: diff --git a/lib/ansible/plugins/lookup/nios.py b/lib/ansible/plugins/lookup/nios.py index 6a090104c92..773ef6f1010 100644 --- a/lib/ansible/plugins/lookup/nios.py +++ b/lib/ansible/plugins/lookup/nios.py @@ -69,7 +69,7 @@ EXAMPLES = """ - name: get a host record set_fact: - host: "{{ lookup('nios', 'record:host', filter={'name': 'hostname.ansible.com'}) }} + host: "{{ lookup('nios', 'record:host', filter={'name': 'hostname.ansible.com'}) }}" - name: get the authoritative zone from a non default dns view set_fact: diff --git a/lib/ansible/plugins/lookup/password.py b/lib/ansible/plugins/lookup/password.py index fadd322ad81..e1bb9f52bf3 100644 --- a/lib/ansible/plugins/lookup/password.py +++ b/lib/ansible/plugins/lookup/password.py @@ -68,7 +68,7 @@ EXAMPLES = """ priv: "{{ client }}_{{ tier }}_{{ role }}.*:ALL" - name: create a mysql user with a random password using only ascii letters - mysql_user: name={{ client }} password="{{ lookup('password', '/tmp/passwordfile chars=ascii_letters') }}" priv='{{ client }}_{{ tier }}_{{ role }}.*:ALL' + mysql_user: name={{ client }} password="{{ lookup('password', '/tmp/passwordfile chars=ascii_letters') }}" priv='{{ client }}_{{ tier }}_{{ role }}.*:ALL' - name: create a mysql user with a random password using only digits mysql_user: diff --git a/lib/ansible/plugins/lookup/vars.py b/lib/ansible/plugins/lookup/vars.py index a5e4f09ba52..9fd77e63af6 100644 --- a/lib/ansible/plugins/lookup/vars.py +++ b/lib/ansible/plugins/lookup/vars.py @@ -40,7 +40,7 @@ EXAMPLES = """ variablename: hello myvar: notename -- name: find several related variables: +- name: find several related variables debug: msg="{{ lookup('vars', 'ansible_play_hosts', 'ansible_play_batch', 'ansible_play_hosts_all') }}" - name: alternate way to find some 'prefixed vars' in loop diff --git a/test/runner/lib/sanity/yamllint.py b/test/runner/lib/sanity/yamllint.py index d86f5875e55..75b42f6068b 100644 --- a/test/runner/lib/sanity/yamllint.py +++ b/test/runner/lib/sanity/yamllint.py @@ -34,6 +34,10 @@ class YamllintTest(SanitySingleVersion): paths = [ [i.path for i in targets.include if os.path.splitext(i.path)[1] in ('.yml', '.yaml')], + [i.path for i in targets.include if os.path.splitext(i.path)[1] == '.py' and + os.path.basename(i.path) != '__init__.py' and + i.path.startswith('lib/ansible/plugins/')], + [i.path for i in targets.include if os.path.splitext(i.path)[1] == '.py' and os.path.basename(i.path) != '__init__.py' and i.path.startswith('lib/ansible/modules/')], diff --git a/test/sanity/yamllint/config/plugins.yml b/test/sanity/yamllint/config/plugins.yml new file mode 100644 index 00000000000..6d418137872 --- /dev/null +++ b/test/sanity/yamllint/config/plugins.yml @@ -0,0 +1,19 @@ +extends: default + +rules: + braces: disable + brackets: disable + colons: disable + commas: disable + comments: disable + comments-indentation: disable + document-start: disable + empty-lines: disable + hyphens: disable + indentation: disable + key-duplicates: disable + line-length: disable + new-line-at-end-of-file: disable + new-lines: {type: unix} + trailing-spaces: disable + truthy: disable diff --git a/test/sanity/yamllint/yamllinter.py b/test/sanity/yamllint/yamllinter.py index e9a4bb95a91..d8926e2238c 100755 --- a/test/sanity/yamllint/yamllinter.py +++ b/test/sanity/yamllint/yamllinter.py @@ -40,6 +40,7 @@ class YamlChecker(object): """ yaml_conf = YamlLintConfig(file='test/sanity/yamllint/config/default.yml') module_conf = YamlLintConfig(file='test/sanity/yamllint/config/modules.yml') + plugin_conf = YamlLintConfig(file='test/sanity/yamllint/config/plugins.yml') for path in paths: extension = os.path.splitext(path)[1] @@ -50,7 +51,12 @@ class YamlChecker(object): if extension in ('.yml', '.yaml'): self.check_yaml(yaml_conf, path, contents) elif extension == '.py': - self.check_module(module_conf, path, contents) + if path.startswith('lib/ansible/plugins/'): + conf = plugin_conf + else: + conf = module_conf + + self.check_module(conf, path, contents) else: raise Exception('unsupported extension: %s' % extension) @@ -137,7 +143,7 @@ class YamlChecker(object): if not module_ast: return {} - if path.startswith('lib/ansible/modules/'): + if path.startswith('lib/ansible/modules/') or path.startswith('lib/ansible/plugins/'): for body_statement in module_ast.body: if isinstance(body_statement, ast.Assign): check_assignment(body_statement, module_doc_types)