{"id":1990,"date":"2021-07-31T12:12:03","date_gmt":"2021-07-31T12:12:03","guid":{"rendered":"https:\/\/linuxdigest.com\/?p=1990"},"modified":"2021-07-31T12:12:04","modified_gmt":"2021-07-31T12:12:04","slug":"ansible-copy-multiple-files","status":"publish","type":"post","link":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/","title":{"rendered":"How to copy multiple files in ansible"},"content":{"rendered":"\n

Copying a single file from your workstation to a target host is one of the most common tasks you run into when using ansible. Ansible has a built-in copy module<\/a> for this that makes it quite simple and straightforward. Here is a typical task to copy a configuration file to the target host.<\/p>\n\n\n\n

- name: Copy the myconf.conf file to the target server\n  copy:\n    src: myconf.conf\n    dest: \/etc\/myconf.conf\n    owner: root\n    group: root\n    mode: u=rw, g=rw, o=r<\/code><\/pre>\n\n\n\n

This will copy myconf.conf from your local machine and place it in \/etc on the target host.<\/p>\n\n\n\n

What if I need to copy a hundred files?<\/h2>\n\n\n\n

This is all fine if you have one file to copy. But if you need to copy a bunch of files, you might not want to create a task for each file. This will make for an unnecessary big playbook. Not to mention the tedious task of copy, pasting, and editing each task. There are a couple of ways to solve this problem.<\/p>\n\n\n\n

Copy the contents of a directory<\/h2>\n\n\n\n

The simplest way to do this is to place all your files in a directory and just copy the directory or its contents. I have seen some complicated ways to copy the contents of a directory to a server. The ansible docs don’t make it very obvious that you don’t need to specify a file name.<\/p>\n\n\n\n

Here is one example where we copy all the files in the myconf\/ directory to \/etc\/myapp\/.<\/p>\n\n\n\n

- name: Copy the contents of the myconf\/ directory to the target server\n  copy:\n    src: myconf\/\n    dest: \/etc\/myapp\/\n    owner: root\n    group: root\n    mode: u=rw, g=rw, o=r<\/code><\/pre>\n\n\n\n

Note the trailing slash in src field. If you skip the slash, ansible will assume you want to copy the directory itself and recursively copy the contents and place it inside the directory. So if we change the src field to this:<\/p>\n\n\n\n

src: myconf<\/code><\/pre>\n\n\n\n

Ansible will create \/etc\/myapp\/myconf\/ and copy the files into that directory.<\/p>\n\n\n\n

The slightly more complicated way<\/h2>\n\n\n\n

The above method is probably the most straightforward way to do this. But if you for some reason don’t want to copy all the files in a directory, you can also create a loop to copy each file one by one.<\/p>\n\n\n\n

In this example, we will do the same as in the example above, copy all the files in a directory to the remote host.<\/p>\n\n\n\n

- name: Copy all the files in myconf\/ but do it with a loop\n  copy:\n    src: {{ item }}\n    dest: \/etc\/myapp\/\n    owner: root\n    group: root\n    mode: u=rw, g=rw, o=r\n   with_fileglob:\n    - \"myconf\/*\"<\/code><\/pre>\n\n\n\n

Why you would want to do it this way is beyond me. But it seems to be a common method of choice for many ansible users. However, using loops to copy files does give you more control over the copy process. For instance, if you just want to copy some files to a destination and not the entire contents of a directory.<\/p>\n\n\n\n

In this example, we will copy three files to \/etc\/myapp<\/p>\n\n\n\n

- name: Copy some files to \/etc\/myapp\/\n  copy:\n    src: {{ item }}\n    dest: \/etc\/myapp\/\n    owner: root\n    group: root\n    mode: u=rw, g=rw, o=r\n   with_items:\n    - db.conf\n    - myapp.conf\n    - cert.crt<\/code><\/pre>\n\n\n\n

Copy multiple files to multiple destinations<\/h2>\n\n\n\n

You can even condense your playbook even more creating just one loop to copy files to multiple destinations. Instead of making a task for each destination folder. Let’s try copying the configuration files for three different apps in one go:<\/p>\n\n\n\n

- name: Copy some files to \/etc\/myapp\/\n  copy:\n    src: {{ item.src }}\n    dest: {{ item.dest }}\n    owner: root\n    group: root\n    mode: u=rw, g=rw, o=r\n   with_items:\n    - { src: app1.conf, dest: \/etc\/app1\/ }\n    - { src: app2.conf, dest: \/etc\/app2\/ }\n    - { src: app3.conf, dest: \/etc\/app3\/ }<\/code><\/pre>\n\n\n\n

Good luck with your project!<\/h2>\n\n\n\n

As in most DevOps tasks, there is more than one way to skin the proverbial cat. I hope I have helped you in some way to find the best way to copy files in your playbook. Happy hacking!<\/p>\n","protected":false},"excerpt":{"rendered":"

Copying a single file from your workstation to a target host is one of the most…<\/p>\n","protected":false},"author":1,"featured_media":1923,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[37],"tags":[40],"yoast_head":"\nHow to copy multiple files in ansible - Linux Digest<\/title>\n<meta name=\"description\" content=\"Copying a single file from your workstation to a target host is one of the most common tasks you run into when using ansible. Ansible has a built-in module for this that makes it quite simple and straightforward. Here is a typical task to copy a configuration file to the target host.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to copy multiple files in ansible - Linux Digest\" \/>\n<meta property=\"og:description\" content=\"Copying a single file from your workstation to a target host is one of the most common tasks you run into when using ansible. Ansible has a built-in module for this that makes it quite simple and straightforward. Here is a typical task to copy a configuration file to the target host.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Digest\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-31T12:12:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-31T12:12:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/Copy-of-Untitled-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"The Linux Digest Guy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"The Linux Digest Guy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/\"},\"author\":{\"name\":\"The Linux Digest Guy\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\"},\"headline\":\"How to copy multiple files in ansible\",\"datePublished\":\"2021-07-31T12:12:03+00:00\",\"dateModified\":\"2021-07-31T12:12:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/\"},\"wordCount\":516,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"keywords\":[\"ansible\"],\"articleSection\":[\"Devops\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/\",\"url\":\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/\",\"name\":\"How to copy multiple files in ansible - Linux Digest\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/#website\"},\"datePublished\":\"2021-07-31T12:12:03+00:00\",\"dateModified\":\"2021-07-31T12:12:04+00:00\",\"description\":\"Copying a single file from your workstation to a target host is one of the most common tasks you run into when using ansible. Ansible has a built-in module for this that makes it quite simple and straightforward. Here is a typical task to copy a configuration file to the target host.\",\"breadcrumb\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/linuxdigest.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to copy multiple files in ansible\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/linuxdigest.com\/#website\",\"url\":\"https:\/\/linuxdigest.com\/\",\"name\":\"Linux Digest\",\"description\":\"Linux tutorials for everyone\",\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/linuxdigest.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/linuxdigest.com\/#organization\",\"name\":\"Linux Digest\",\"url\":\"https:\/\/linuxdigest.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"contentUrl\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"width\":1102,\"height\":170,\"caption\":\"Linux Digest\"},\"image\":{\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\",\"name\":\"The Linux Digest Guy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"caption\":\"The Linux Digest Guy\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to copy multiple files in ansible - Linux Digest","description":"Copying a single file from your workstation to a target host is one of the most common tasks you run into when using ansible. Ansible has a built-in module for this that makes it quite simple and straightforward. Here is a typical task to copy a configuration file to the target host.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/","og_locale":"en_US","og_type":"article","og_title":"How to copy multiple files in ansible - Linux Digest","og_description":"Copying a single file from your workstation to a target host is one of the most common tasks you run into when using ansible. Ansible has a built-in module for this that makes it quite simple and straightforward. Here is a typical task to copy a configuration file to the target host.","og_url":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/","og_site_name":"Linux Digest","article_published_time":"2021-07-31T12:12:03+00:00","article_modified_time":"2021-07-31T12:12:04+00:00","og_image":[{"width":350,"height":200,"url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/Copy-of-Untitled-2.jpg","type":"image\/jpeg"}],"author":"The Linux Digest Guy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"The Linux Digest Guy","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/#article","isPartOf":{"@id":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/"},"author":{"name":"The Linux Digest Guy","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2"},"headline":"How to copy multiple files in ansible","datePublished":"2021-07-31T12:12:03+00:00","dateModified":"2021-07-31T12:12:04+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/"},"wordCount":516,"commentCount":0,"publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"keywords":["ansible"],"articleSection":["Devops"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/","url":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/","name":"How to copy multiple files in ansible - Linux Digest","isPartOf":{"@id":"https:\/\/linuxdigest.com\/#website"},"datePublished":"2021-07-31T12:12:03+00:00","dateModified":"2021-07-31T12:12:04+00:00","description":"Copying a single file from your workstation to a target host is one of the most common tasks you run into when using ansible. Ansible has a built-in module for this that makes it quite simple and straightforward. Here is a typical task to copy a configuration file to the target host.","breadcrumb":{"@id":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/linuxdigest.com\/howto\/ansible-copy-multiple-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxdigest.com\/"},{"@type":"ListItem","position":2,"name":"How to copy multiple files in ansible"}]},{"@type":"WebSite","@id":"https:\/\/linuxdigest.com\/#website","url":"https:\/\/linuxdigest.com\/","name":"Linux Digest","description":"Linux tutorials for everyone","publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxdigest.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxdigest.com\/#organization","name":"Linux Digest","url":"https:\/\/linuxdigest.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","contentUrl":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","width":1102,"height":170,"caption":"Linux Digest"},"image":{"@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2","name":"The Linux Digest Guy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","caption":"The Linux Digest Guy"}}]}},"_links":{"self":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1990"}],"collection":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/comments?post=1990"}],"version-history":[{"count":1,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1990\/revisions"}],"predecessor-version":[{"id":1991,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1990\/revisions\/1991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media\/1923"}],"wp:attachment":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media?parent=1990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/categories?post=1990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/tags?post=1990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}