• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to resolve 'dict object' has no attribute 'files' error in Ansible playbook?

 
Greenhorn
Posts: 13
  • Number of slices to send:
    Optional 'thank-you' note:
I'm working on an Ansible playbook to set permissions for initialization files in user home directories to meet STIG requirements. However, I'm running into an issue with the 'Correct file permissions' task, where I get the following error:

fatal: [SERVER_NAME]: FAILED! => { "msg": "The conditional check 'init_files.results | map(attribute='files') | flatten | length > 0' failed. The error was: Could not load \"length\": 'length'\n line 0\n\nThe error appears to be in '/home/path/to/playbook/tasks/playbook name.yml': line 21, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Correct file permissions\n ^ here\n" }  

Here's the snippet of the playbook causing the issue:

- name: Set permissions for initialization files in user home directories

find:  

paths: "/home/{{ item }}"

patterns: ".*"  

file_type: file  

hidden: yes  

register: init_files  

loop: "{{ users.stdout_lines }}"  

- name: Correct file permissions

file:

path: "{{ item.path }}"

mode: '0740'  

loop: "{{ init_files.results | map(attribute='files') | flatten }}"  

when: init_files.results | map(attribute='files') | flatten | length > 0  

The task before 'Correct file permissions' is supposed to find all hidden files within the user's home directory and register the results. I expected to loop over these results to correct file permissions but it seems like the init_files variable is not structured as anticipated. I've attempted to debug with a var output, but the issue persists.

Can someone help me understand why the init_files variable isn't structured with a files attribute as the find module documentation suggests, or point out what I might be doing wrong?

   Here is what I have tried: Checked for invisible characters or issues with line endings using :set list in vi.

   Ensured that the indentation is consistent throughout the playbook.

   Replaced select('length') | list | length with flatten | length in the when conditional.

   Verified the Ansible and Jinja2 documentation for the correct syntax and usage of filters and conditionals.

Stig: Check Text: Verify that all local initialization files have a mode of "0740" or less permissive with the following command:

Note: The example will be for the "smithj" user, who has a home directory of "/home/smithj".

$ sudo ls -al /home/smithj/.[^.]* | more

-rw-------. 1 smithj users 2984 Apr 27 19:02 .bash_history

-rw-r--r--. 1 smithj users 18 Aug 21 2019 .bash_logout

-rw-r--r--. 1 smithj users 193 Aug 21 2019 .bash_profile

If any local initialization files have a mode more permissive than "0740", this is a finding.

Any help or resources for troubleshooting this playbook would be greatly appreciated.
 
Bartender
Posts: 217
7
  • Number of slices to send:
    Optional 'thank-you' note:
First of all I think YAML code should be formatted. Otherwise it's nearly unreadable. I noticed your question already some days ago but found an answer only after copy&pasting the code to an editor supporting YAML.

The SW running the Coderanch site does not support YAML but formatting as text makes the it already easy to understand and preserves whitespace.

My findings are:
In task "Set permissions for initialization files in user home directories"  the register directive must be after the loop. Otherwise Ansible thinks its should be a parameter of find

The task "Correct file permissions" then works with or without the when condition. The when can be omitted. In case there are no matched files the action indie the loop is simply not carried out.




My test dir '{{ SLASH_HOME}}' after test run. permisions of the .a files that are directly under their home dir are changed, but other files not.

 
Saloon Keeper
Posts: 27868
196
  • Number of slices to send:
    Optional 'thank-you' note:
When in doubt, use Code Tags for formatted text. It will at least preserve formatting, even if the language in question is not supported. And PLEASE don't double-space!

I have found that conditional expressions can fail in ansible if you're invoking functionality that may not always produce results. That appears to be the case here, where the results of "init_files.results | map(attribute='files') | flatten" must be returning no data rather than an empty collection (which would have a length of 0 for that case).

The only cure for that is to make tests that won't fail and that can be a bit of trial-and-error if you cannot find good documentation.

Offhand, I'd omit the "when" clause here, since iterating over nothing does nothing.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |