Hi guys,
I’ve got something I’d really like some help on, not just the solution, but the understanding and perhaps any related documentation/articles.
I’m writing a role for deployment of Hubot.
To define bots to be deployed, I’m using a dictionary, like so:
`
hubot_bots:
testbot:
owner: ‘Bot Wrangler bw@example.com’
name: Hubot
descr: Delightfully aware robutt
adapter: rocketchat
environment:
- rocketchat_room: GENERAL
- rocketchat_user: Hubot
- rocketchat_password: Hubot
`
Using ‘with_items’ I can iterate over items in ‘hubot_bots’, like ‘testbot’ here, and create resources, such as directories named after the key ‘testbot’ and then run commands accessing the values using ‘item.value.owner’ and ‘item.value.adapter’, etc.
Now, for each of these bot definitions, I want to deploy a Systemd service file, for management of each bot process.
Within this template, I want to refer to some of the values as defined above, including iterating over each key/value in the ‘environment’ list.
I tried my best the other evening with a few combinations of ‘with_dict’ and ‘with_nested’ and felt I was making some progress, but definitely not getting the desired results, nor understanding how it was working.
Here’s the task I’m deploying the template with:
`
- name: Deploy service files for each Hubot
template:
src: hubot.service.j2
dest: /usr/lib/systemd/system/hubot-{{ item.key }}.service
owner: root
group: root
mode: 0644
with_dict: hubot_bots
`
And here’s the template I’m using:
`
[Unit]
Description=Hubot-{{ item }}
Requires=network.target
After=network.target
[Service]
Type=simple
WorkingDirectory={{ hubot_village_path }}/{{ item }}
User={{ hubot_admin_user }}
Restart=always
RestartSec=10
{% for env in item.value.environment %}
Environment={{ env|upper}}={{ env.value }}
{% endfor %}
ExecStart={{ hubot_village_path }}/{{ item }}/bin/hubot --adapter {{ item.value.adapter }}
[Install]
WantedBy=multi-user.target
`
As you can see, I want to refer to each bot defined under ‘hubot_bots’ and then some other values.
Would anyone be able to point me in the right direction on how to get the desired outcome, and understand how this works.
Any documentation or articles would be great. I’m still grasping concepts with YAML structures and the Jinja2 template engine - I’d love to learn as much as I can about it, as I can see it can be very powerful.
So, thank you so much in advance if anyone can help me on this!
Kind Regards,
Calum