linux - File copy in ansible failed! =>{"msg": "'dict object' has no attribute 'files'"} - TagMerge
5File copy in ansible failed! =>{"msg": "'dict object' has no attribute 'files'"}File copy in ansible failed! =>{"msg": "'dict object' has no attribute 'files'"}

File copy in ansible failed! =>{"msg": "'dict object' has no attribute 'files'"}

Asked 1 years ago
2
5 answers

The following solution will does not use the with_items option. It will recursively find all the files which ends with the extension/suffix .ign.

      - name: Find files                                                         
        find:                                                                    
          paths: "/path/to/directory"
          patterns: "*.ign"                                             
          recurse: yes                                                           
        register: result                                                         
                                                                                 
      - name: Print find result 
        debug:                                                                   
          msg: "{{ item.path }}"                                                 
        with_items:                                                              
          - "{{ result.files }}"  

Source: link

0

An explanation regarding your given find file loop

  with_items:
    - "{{ workdir }}/*.ign"
  register: find_result

and the given error message

msg": "/root/openstack-upi/*.ign was skipped as it does not seem to be a valid directory or it cannot be accessed

It will be necessary to lookup the files matching a pattern before. This can be done with the with_fileglob lookup plugin.

  with_fileglob:
    - "{{ workdir }}/*.ign"
  register: find_result

or better and as explained in the accepted answer here in this thread.

The error message just sayed the path and filename were skipped as .../*.ign wasn't a valid path and filename. It means it hasn't looked up any files and hasn't resolved the fileglob.

Source: link

0

- name: example copying file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: 0644

- name: The same example as above, but using a symbolic mode equivalent to 0644
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u=rw,g=r,o=r

- name: Another symbolic mode example, adding some permissions and removing others
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u+rw,g-wx,o-rwx

- name: Copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
  copy:
    src: /mine/ntp.conf
    dest: /etc/ntp.conf
    owner: root
    group: root
    mode: 0644
    backup: yes

- name: Copy a new "sudoers" file into place, after passing validation with visudo
  copy:
    src: /mine/sudoers
    dest: /etc/sudoers
    validate: /usr/sbin/visudo -cf %s

- name: Copy a "sudoers" file on the remote machine for editing
  copy:
    src: /etc/sudoers
    dest: /etc/sudoers.edit
    remote_src: yes
    validate: /usr/sbin/visudo -cf %s

- name: Copy using the 'content' for inline data
  copy:
    content: '# This file was moved to /etc/other.conf'
    dest: /etc/mine.conf'

Source: link

0

By default, the copy module will check the file set in the src parameter, on the local machine. And then it will copy the file to the remote machine path specified in the dest path. The example below will copy the sample.txt file in the home directory of the current user( on the local machine), to the /tmp directory on the remote server. Since we are not specifying any permission for the file, the default permission for the remote file is set as -rw-rw-r–(0664).
- hosts: blocks
  tasks:
  - name: Ansible copy file to remote server
    copy:
      src: ~/sample.txt
      dest: /tmp
Note 1: If the file is already present on the remote server and if the source file’s content is different, then on running the task, the destination file will be modified. You can control this by setting the force parameter. The default is set to yes’. So it modifies the file by default. If you don’t want the file to be modified if the source file is different, then you can set it to ‘No’. The following task will only copy the file if the file does not exist on the remote server.
- hosts: blocks
  tasks:
  - name: Ansible copy file force
    copy:
      src: ~/sample.txt
      dest: /tmp 
      force: no
The below Ansible copy directory example will first create a directory named  copy_dir_ex in the /tmp of the remote server. See how there is a ‘copy_dir_ex’ folder inside the ‘tmp’ folder.
- hosts: blocks
  tasks:
  - name: Ansible copy directory to the remote server
    copy:
      src:/Users/mdtutorials2/Documents/Ansible/copy_dir_ex
      dest:/Users/mdtutorials2/Documents/Ansible/tmp

output
------
Ansible-Pro:Ansible mdtutorials2$ tree tmp
tmp
└── copy_dir_ex
    ├── file1
    ├── file2
    ├── file3
    └── tmp2
        ├── file4
        └── file5
In the below example the files inside the copy_dir_ex will be copied to the /tmp folder of the remote server. As you can see the src directory is not created in the destination. Only the contents of the directory are copied.
- hosts: blocks
  tasks:
  - name: Ansible copy files from a directory to remote server
    copy:
      src:/Users/mdtutorials2/Documents/Ansible/copy_dir_ex/
      dest:/Users/mdtutorials2/Documents/Ansible/tmp

output
------
tmp/
├── file1
├── file2
├── file3
└── tmp2
    ├── file4
    └── file5
The following example copies the hello6 file in the /tmp directory of the remote server and pastes it in the /etc/ directory.
- hosts: blocks
  tasks:
  - name: Ansible copy files remote to remote
    copy:
      src: /tmp/hello6
      dest: /etc
      remote_src: yes

Source: link

0

Problem: Ansible is interrupted with the following error:
TASK [suricata-update single rules file upload] ********************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: If you are using a module and expect the file to exist on the remote, see the remote_src option
fatal: [remote1]: FAILED! => {"changed": false, "msg": "Could not find or access '/var/lib/suricata/suricata.rules' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
Ansible Task
- name: Suricata installation
  hosts: detection
  become: true
  become_user: root

tasks:
###using suricata-update single rules file
  - name: suricata-update single rules file upload
    copy: src=/var/lib/suricata/suricata.rules
               dest=/etc/suricata/rules/suricata.rules
               remote_src=no
               mode=preserve
    notify: restart suricata
How it worked: I have chowned everything below /var/lib/suricata/ as follows:
chown -R guimgmt:guimgmt /var/lib/suricata/
Code blocks
~~~
Code surrounded in tildes is easier to read
~~~

Source: link

Recent Questions on linux

    Programming Languages