Hello,
I have a template with few variables to be populated, then I am shipping that template populated to a another server.
Here is my playbook :
- hosts: dev
become: yes
become_method: sudo
vars:
username: “{{ username }}”
ad_username: “{{ ad_username }}”
vhost_id: “{{ vhost_id }}”
tasks:
- name: Ansible Template
template:
src: /usr/local/cmd-sysadmin/automation/ansible/cmd/files/templates/apache_vhost_api_private_public.j2
dest: /usr/local/apache2.4/conf/vhosts/v2/{{ vhost_id }}_{{ username }}_apis.vhost.conf
- name: Add vhost to httpd.conf
shell: echo “Include conf/vhosts/v2/{{ vhost_id }}_{{ username }}_apis.vhost.conf” >> /usr/local/apache2.4/conf/httpd.conf
It works well if I call it from command line and I pass all variable via -e switch.
Something like : ansible-playbook playbooks/apache/create_new_vhost_from_template_dev.yml -K -e “username=xxxx ad_username=xxxx.xxx vhost_id=60”
As I have too many vhosts to be created, I got a file from where I would like to read content, iterate it, populate variables and push the file.
The file is like this :
5;yyy;yyyyy
70;zzzz;zzz
I can modify the file to include the header, to be , delimited… whatever.
Is there anyone that can explain how to do this?
It’s hard for me to find something like this on the documentation.
Can anyone help?
Thanks
Hi Nicola,
As I have too many vhosts to be created, I got a file from where I would
like to read content, iterate it, populate variables and push the file.
The file is like this :
5;yyy;yyyyy
70;zzzz;zzz
I can modify the file to include the header, to be , delimited.. whatever.
Since this is basically a CSV file, you might have a look at the
read_csv module.
https://docs.ansible.com/ansible/latest/modules/read_csv_module.html
This is only available in Ansible 2.8 though, for older versions you
might use the csvfile lookup.
https://docs.ansible.com/ansible/latest/plugins/lookup/csvfile.html
Regards
Sebastian
Hey Sebastian,
thanks for your reply.
But once I read from the csv how do I get the variables populated and how to iterate?
I have something like this now
- hosts: dev
become: yes
become_method: sudo
vars:
username: “{{ users.0 }}”
ad_username: “{{ users.1 }}”
vhost_id: “{{ users.2 }}”
tasks:
- name: Read users from CSV file and return a list
read_csv:
path: /home/ncontu/users.csv
delimiter: ‘;’
register: users
- name: Ansible Template
template:
src: /usr/local/cmd-sysadmin/automation/ansible/cmd/files/templates/apache_vhost_api_private_public.j2
dest: /usr/local/apache2.4/conf/vhosts/v2/{{ vhost_id }}_{{ username }}_apis.vhost.conf
loop: users
- name: Add vhost to httpd.conf
shell: echo “Include conf/vhosts/v2/{{ vhost_id }}_{{ username }}_apis.vhost.conf” >> /usr/local/apache2.4/conf/httpd.conf
loop: users
I found the way to loop and get variables.
Thanks a lot
It's a common courtesy to also provide the solution.
Think about the next person that has the same challenge as you only to find your post that basically only say solved it.
That’s exactly what I’m looking for: how he solved his issue. Unfortunately, he only wanted to solve his problem, but he didn’t care about others having the same problem