{"id":2282,"date":"2021-02-08T18:22:04","date_gmt":"2021-02-08T15:22:04","guid":{"rendered":"https:\/\/artem.services\/?p=2146"},"modified":"2022-06-08T22:28:35","modified_gmt":"2022-06-08T19:28:35","slug":"2282","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=2282&lang=en","title":{"rendered":"\u00a0Ansible &#8212; Store N recent directories and artifacts"},"content":{"rendered":"<h3>Goal:<\/h3>\n<p><span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"0\" data-number-of-phrases=\"3\"><span class=\"Q4iAWc\">There is a directory &quot;<strong>\/opt\/application<\/strong>&quot; where the archive with the application is downloaded and unpacked into a directory where the short derivative of the HASH commit (8 characters) is used as the name.<\/span><\/span> <span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"1\" data-number-of-phrases=\"3\"><span class=\"Q4iAWc\">And a symbolic link is created to this directory.<\/span><\/span> <span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"2\" data-number-of-phrases=\"3\"><span class=\"Q4iAWc\">It is necessary to store only the 3 latest versions of the application, both directories, and archives, and also do not delete directories called:<\/span><\/span><\/span><\/p>\n<ul>\n<li>logs<\/li>\n<li>media<\/li>\n<\/ul>\n<p><span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"0\" data-number-of-phrases=\"1\"><span class=\"Q4iAWc\">Also, the deletion should be done using <strong>regex<\/strong>, so that in the future it does not delete directories that may be created.<\/span><\/span><\/span><\/p>\n<h3>Playbook:<\/h3>\n<pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\r\n---\r\n- hosts: all\r\n  tasks:\r\n    - name: Find all application directories\r\n      find:\r\n        paths: &quot;\/opt\/application\/&quot;\r\n        file_type: directory\r\n        use_regex: yes\r\n        patterns:\r\n          - &#039;[a-z0-9]{8,8}&#039;\r\n        excludes: &#039;logs,media&#039;\r\n      register: dirs\r\n\r\n    - name: Keep only the last 3 directories\r\n      file:\r\n        path: &quot;{{ item }}&quot;\r\n        state: absent\r\n      with_items: &quot;{{ (dirs.files | sort(attribute=&#039;atime&#039;, reverse=True))[3:] | map(attribute=&#039;path&#039;) | list }}&quot;\r\n\r\n    - name: Find application artifacts\r\n      find:\r\n        paths: &quot;\/opt\/application\/&quot;\r\n        file_type: file\r\n        use_regex: yes\r\n        patterns:\r\n          - &#039;^[a-z0-9]{8,8}.tar.gz$&#039;\r\n      register: artifacts\r\n\r\n    - name: Keep only the last 3 artifacts\r\n      file:\r\n        path: &quot;{{ item }}&quot;\r\n        state: absent\r\n      with_items: &quot;{{ (artifacts.files | sort(attribute=&#039;atime&#039;, reverse=True))[3:] | map(attribute=&#039;path&#039;) | list }}&quot;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Goal: There is a directory &quot;\/opt\/application&quot; where the archive with the application is downloaded and unpacked into a directory where the short derivative of the HASH commit (8 characters) is used as the name. And a symbolic link is created to this directory. It is necessary to store only the 3 latest versions of the &hellip; <a href=\"https:\/\/artem.services\/?p=2282&#038;lang=en\" class=\"more-link\">\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u044c<span class=\"screen-reader-text\"> &quot;\u00a0Ansible &#8212; Store N recent directories and artifacts&quot;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1131],"tags":[1133],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2282"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2282"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2282\/revisions"}],"predecessor-version":[{"id":2284,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2282\/revisions\/2284"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}