Answers: <p>Ok, solved it :)</p>
  
  <p>Here the task for everyone who intends to do the same.</p>
  
  <p>Only the skip/changed console output could be replaced by a nicer message.</p>
  
  <pre><code>- name: Register unwanted motd files
    stat:
      path: "{{motd_path}}/{{item}}"
    register: filecheck
    with_items: "{{ unwanted_motd_files | default([]) }}"
  
  - name: Check if unwanted motd files are executable and remove the executable bit
    file:
      path: "{{motd_path}}/{{item.item}}"
      state: touch
      mode: u-x,g-x,o-x
    with_items: "{{ filecheck.results }}"
    when: item.stat.exists == true and item.stat.executable == True
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-09-19T16:40:51.003Z
Answers: 2019-09-19T16:40:51.003Z
Answers: tdoe
Body: <p>I'd like to remove the execute bit from some ubuntu motd (Message of the day) files with ansible to disable some ad-like Ubuntu news.
  The module should only act if files exist and permissions are wrong.</p>
  
  <p>If there are no files the tasks are running fine, but as soon as a file exists, I'm getting this:</p>
  
  <p><strong>Error Message:</strong></p>
  
  <pre><code>failed: [ip] (item={'_ansible_parsed': True, u'stat': {u'isuid': False, u'uid': 0, u'exists': True, u'attr_flags': [... redacted the rest of the file attributes from stat ...]
  </code></pre>
  
  <p>Mayboe someone can help?</p>
  
  <p>Here the files of my role:</p>
  
  <p><strong>defaults/main.yml</strong></p>
  
  <pre><code>---
  motd_path: '/etc/update-motd.d'
  unwanted_motd_files:
    - 10-help-text
    - 50-motd-news
  </code></pre>
  
  <p><strong>tasks/main.yml</strong></p>
  
  <pre><code>---
  - name: Register unwanted motd files
    stat:
      path: "{{motd_path}}/{{item}}"
    register: filecheck
    with_items: "{{ unwanted_motd_files | default([]) }}"
  
  - name: Check if unwanted motd files are executable and remove the executable bit
    file:
      path: "{{motd_path}}/{{ item }}"
      state: touch
      mode: u-x,g-x,o-x
    with_items: "{{ filecheck.results }}"
    when: item.stat.exists == true
  </code></pre>
  
Tags: <ansible>
Title: Loop chmod with Ansible
UserDisplayName: tdoe
ThreadText: # Loop chmod with Ansible
  
  (Asked by: username_0 on 2019-09-19)
  
  I'd like to remove the execute bit from some ubuntu motd (Message of the day) files with ansible to disable some ad-like Ubuntu news.
  The module should only act if files exist and permissions are wrong.
  
  If there are no files the tasks are running fine, but as soon as a file exists, I'm getting this:
  
  **Error Message:**
  
  ```
  failed: [ip] (item={'_ansible_parsed': True, u'stat': {u'isuid': False, u'uid': 0, u'exists': True, u'attr_flags': [... redacted the rest of the file attributes from stat ...]
  
  ```
  
  Mayboe someone can help?
  
  Here the files of my role:
  
  **defaults/main.yml**
  
  ```
  ---
  motd_path: '/etc/update-motd.d'
  unwanted_motd_files:
    - 10-help-text
    - 50-motd-news
  
  ```
  
  **tasks/main.yml**
  
  ```
  ---
  - name: Register unwanted motd files
    stat:
      path: "{{motd_path}}/{{item}}"
    register: filecheck
    with_items: "{{ unwanted_motd_files | default([]) }}"
  
  - name: Check if unwanted motd files are executable and remove the executable bit
    file:
      path: "{{motd_path}}/{{ item }}"
      state: touch
      mode: u-x,g-x,o-x
    with_items: "{{ filecheck.results }}"
    when: item.stat.exists == true
  
  ```
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-09-19. Score: 2
  
  Ok, solved it :)
  
  Here the task for everyone who intends to do the same.
  
  Only the skip/changed console output could be replaced by a nicer message.
  
  ```
  - name: Register unwanted motd files
    stat:
      path: "{{motd_path}}/{{item}}"
    register: filecheck
    with_items: "{{ unwanted_motd_files | default([]) }}"
  
  - name: Check if unwanted motd files are executable and remove the executable bit
    file:
      path: "{{motd_path}}/{{item.item}}"
      state: touch
      mode: u-x,g-x,o-x
    with_items: "{{ filecheck.results }}"
    when: item.stat.exists == true and item.stat.executable == True
  
  ```
  
  
  ---
  
  