Ansible: Create Config based on File List - automation

I'm a little confused on how to make this work and need a better understanding in terms of the ansible side of getting the following done:
I have 500 images and I need to generate a default json config based on the image names. For example:
1.jpg
2.jpg
3.jpg
etc etc
From that list, I need to generate the following:
1.json
2.json
3.json
etc etc
When it's all said and done the directory should have 500 images and 500 json files. I would also like to have that json file as a j2 template so I can pre define some information in the json file based on the project in a group_var.
I know I can do the following to copy the template doing this:
- name: Copy JSON Configuration
ansible.builtin.template:
src: sample.json.j2
dest: /path/to/directory
I'm just lost in the part to generate the list of the json files based on the image list. I have googled some stuff that is maybe the same but what I have found seem way beyond what I needed to get done or I'm not simply understanding it correctly. Thank you for any and all help I really appreciate it!

For example, given the tree
shell> tree test-616
test-616
├── 1.jpg
├── 2.jpg
└── 3.jpg
and the template
shell> cat sample.json.j2
{{ item }}
Find the files and iterate the paths
- find:
path: test-616
register: result
- template:
src: sample.json.j2
dest: "{{ _item }}.json"
loop: "{{ result.files|map(attribute='path')|list }}"
vars:
_item: "{{ item|splitext|first }}"
This will create the files
shell> tree test-616
test-616
├── 1.jpg
├── 1.json
├── 2.jpg
├── 2.json
├── 3.jpg
└── 3.json

Related

vite - Subpage with relative asset path

I have a Vue project with multiple pages where I use rollupOptions.input to specify them as entry points:
rollupOptions: {
input: {
main: resolve(__dirname, "index.html"),
subpage1: resolve(__dirname, "subpage1/index.html"),
subpage2: resolve(__dirname, "subpage2/index.html")
}
},
The final dist folder will be deployed at a subdirectory in a server,
so I then set a base attribute as base: "", to make the assets work for the main index.html . This turns all the paths into something relative like this: <link rel="stylesheet" href="assets/main.35431485.css">. Works for the root index.html but for the subpages, the links look identical. This however doesn't work, because the folder structure is something like:
├── index.html
├── assets
├── main.35431485.css
└── ...
└── subpage1
└── index.html
As such, subpage1/assets/main.35431485.css will simply not work.
Is there a way to tell vite to relatively path its way to the asset folder, even for subpages?
Ideally not using a static parent directory (like with base: "/some/dir/"), but keeping it all relative?
This is a known issue that will be fixed in vite 3.0. There seems to be a fair amount of issues left for the 3.0 release but hopefully it will be released sometime in 2022. Right now the only way is to have a static base path, changing to some SPA approach, or using another build tool.
Edit: vite 3.0 is now released and relative base path is now properly supported. See the docs here.

I cannot change local json file in runtime

I want to use a local json file in my app. I simply import it and use without any problem in development. But after build, it's bundled and I cannot see or change it.
I don't want it to be bundled, so I can change it while my app is working on my host.
or any other suggestion is appreciated.
<script>
import articlesJson from "./../static/articles.json";
export default {
name: "Body",
data() {
return {
articles: articlesJson
};
},
};
</script>
My dist folder after build.
.
├── css
│   ├── app.934f0703.css
│   └── chunk-vendors.e246dba9.css
├── fav.ico
├── img
│   └── profile-photo.446da51d.jpg
├── index.html
└── js
├── app.01988997.js
├── app.01988997.js.map
├── chunk-vendors.3ee1fa24.js
└── chunk-vendors.3ee1fa24.js.map
You should be able to import the data in your json file if you convert it to a .js file and declare and export your json object.
const myObject = { 'property': 'value' }
export default myObject;
Then you should be able to use it like any other module. Alternately you may need a some sort of a json loader... webpack docs
Edit
I just saw that your problem isn't importing the file but updating it. If you want your app to see the up to date version you'll need to reference that file directly rather than importing the module from the bundle. The bundle won't be updated without your repacking/deploying.
I'm not sure how you're serving your app, but in addition to referencing your json file directly, you need to make sure that file is deployed to the server rather than simply hanging out with the rest of your unbundled js which shouldn't be deployed.

hexo change folder structure when generate static files

First of all, thanks Hexo. Here is my question:
I set post_asset_folder to true in the Hexo configuration file. Then I run:
$ hexo new first.
then:
$ ls source/_posts/
first/ first.md hello-world.md
I added a pic named pic.png into source/_posts/first and wrote something in source/_posts/first.md like the following:
title: first
date: 2015-06-16 13:42:29
tags:
---
picture blow ^_^
![pic](first/pic.png)
Then:
$ hexo g
$ hexo s
I open http://0.0.0.0:4000/, but i coundn't see the content of pic.png.
I checked the folder public/2015/06/16/first/. I found there is some diffrience between folder public/2015/06/16/ and folder source/_posts/.
structure of folder public/2015/06/16/
.
└── public/2015/06/16/
├── first
│ ├── pic.png
│ └── first.md
└── hello-world
└── hello-world.md
structure of folder source/_posts/
.
└── source/_posts/
├── first
│ ├── first
│ │ └── pic.png
│ └── first.md
└── hello-world
└── hello-world.md
How can i unify the path format that i could get same path in markdowm and index.html.
You can (sadly) not display an image in your markdown editor of choice and the rendered HTML file. To make it work in in the rendered version, you have to address it like this: ![](cat.jpg).
Set post_asset_folder: true in _config.yml
Create new post with hexo new post "Some New Post"
Place image in source/_posts/Some-New-Post/, e.g. source/_posts/Some-New-Post/cat.jpg
Display image in post with ![](cat.jpg)
in your post, just use pic.png, but not first/pic.png
hexo will put the files in asset folder with html file together
This is not the right way to create post with photos. Make that post_assets_folder is set to true and follow these steps :
1. Run hexo new post "YOUR TITLE" (a folder YOUR-TITLE and a file YOUR-TITLE.md are created in source/_posts)
2. Place your photos in source/_posts/YOUR-TITLE folder
3. To link photos in a post, you have to use relative url, so your url will be directly the title of your photos (e.g: pic.png)
The directory structure in the question is incorrect.
Here are my steps:
create a post:
hexo new first
modify the post' Markdown file:
vim source/_post/first.md
content of first.md:
title: first
date: 2015-06-16 13:42:29
tags:
---
picture blow ^_^
![pic](first/pic.png)
add image pic.png to the source/_post/first folder
now the structure of source directory looks like this:
- source
`- _posts/
`- first
`pic.png
`- first.md
`- hello-world.md
run hexo g && hexo s
open http://0.0.0.0 in a browser; note that the pic.png is not shown
the public directory looks like this:
- public
`- 2015
`- 06
`- 16
`- first
`- index.html
`- pic.png
`- hello-world
`- index.html
From there you can see that the pic.png file was moved from the subdirectory first/first to first.
So in order to make it work, you need to refer to the file relative to the post directory (first, e.g. {% asset_img 'pic.png' %} should do the trick).
in hexo 3 the recommended way to reference a asset image is by tag plugin, rather than markdown. Check the docs
{% asset_img "fudan-advanced-math-01-01.PNG"%}
![](fudan-advanced-math-01-01.PNG) // doesn't work well

Testing Chef roles and environments

I'm new to Chef and have been using Test Kitchen to test the validity of my cookbooks, which works great. Now I'm trying to ensure that environment-specific attributes are correct on production nodes prior to running Chef initially. These would be defined in a role.
For example, I may have recipes that converge using a Vagrant box with dev settings, which validates the cookbook. I want to be able to test that a production node's role. I think I want these tests as the source of truth describing my environment. Looking at Test Kitchen's documentation, this seems beyond its scope.
Is my assumption correct? Is there a better approach to test a cookbook before the first time Chef is run on a production node to ensure it has the correct settings?
I pleasantly discovered that chef_zero uses the "test/integration" directory as it's chef repository.
Just create your roles under
test/integration/roles
Example
Standard Chef cookbook layout.
├── attributes
│   └── default.rb
├── Berksfile
├── Berksfile.lock
├── chefignore
├── .kitchen.yml
├── metadata.rb
├── README.md
├── recipes
│   └── default.rb
└── test
└── integration
├── default
│   └── serverspec
│   ├── default_spec.rb
│   └── spec_helper.rb
└── roles
└── demo.json
.kitchen.yml
---
driver:
name: vagrant
provisioner:
name: chef_zero
platforms:
- name: ubuntu-14.04
suites:
- name: default
run_list:
- role[demo]
attributes:
Notes:
Provisioner is chef_zero
The runlist is configured to use a role
recipes/default.rb
file "/opt/helloworld.txt" do
content "#{node['demo']['greeting']}"
end
attributes/default.rb
default['demo']['greeting'] = "hello world"
Notes:
Cookbook won't compile without a default
test/integration/default/serverspec/default_spec.rb
require 'spec_helper'
describe file('/opt/helloworld.txt') do
it { should be_file }
its(:content) { should match /this came from my role/ }
end
Notes:
Integration test is looking for the content that is set by the role attribute
test/integration/roles/demo.json
{
"name": "demo",
"default_attributes": {
"demo": {
"greeting": "this came from my role"
}
},
"run_list": [
"recipe[demo]"
]
}
You can set both roles and environments in your .kitchen.yml, so you certainly can test this with test kitchen.
....
provisioner:
roles_path: path/to/your/role/files
client_rb:
environment: your_environment
.....
That said, I personally prefer to use role cookbooks. If you have a fixed set of environments, as we do, then you can also use simple conditionals in the attributes files of your role cookbook to adjust attributes based on environment too. That way, you have a single cookbook that defines the entire configuration of your node by wrapping other cookbooks and setting variables. With that setup, it is very easy to setup kitchen tests that validate the exact production system.
When coming to validating attributes the part of Test Kitchen your should be using is ChefSpec.
You can define a complete runlist in a spec file and ensure the rendered files are correct.
There's a part of Chefspec documentation about it here.
Another way to do this is to have a "role cookbook", instead of using a role on chef server, you define the attributes you wish to define in an attribute file and make this cookbook depends on what the role runlist would be.
This role cookbook recipe would have include_recipe only referencing the recipe you would have set in the role runlist.
The main advantage here is that you can include your specs in this cookbook independently of the referenced cookbooks.

How to use grunt-contrib-copy to copy to root AND change directory structure

I have an express app with my dev views in /assets/views. I figure I need to separate development and production views because in production I'll be editing the HTML when I used grunt-contrib-usemin to concat/uglify scripts.
So here's the problem. My current tree:
assets/views
├── 404.html
├── index.html
├── layout.html
├── question_ask.html
└── question_display.html
Ideally, I want my production-ready views to live on the same level as assets. Using grunt-contrib-copy, it seems to copy the whole tree. I currently am putting it into public since I'm not sure how to set my dest to the root of the project.
copy: {
views: {
src: ['assets/views/*.html'],
dest: 'public/'
}
So there are a few questions here:
Is it bad practice to have dev views and production views? If so, is there another way of producing a view that has references to concat/uglified scripts?
How the heck can I use grunt-contrib-copy to copy to the root of my project? I don't want assets/views obviously, I just want a views/* folder that has the contents of what's in assets/views/*.
Thanks!
You need to specify the flatten option which will remove the directory structure from the destination path. See my answer here: Copy all files from directory to another with Grunt.js copy