Ansible iterate from file and populate template

25 views
Skip to first unread message

Nicola Contu

unread,
Jun 13, 2019, 9:06:00 AM6/13/19
to Ansible Project
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

Sebastian Meyer

unread,
Jun 13, 2019, 9:50:59 AM6/13/19
to ansible...@googlegroups.com
Hi Nicola,

On 13.06.19 15:06, Nicola Contu wrote:
> 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

--
Sebastian Meyer
Linux Consultant & Trainer
Mail: me...@b1-systems.de

B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537

Nicola Contu

unread,
Jun 13, 2019, 10:16:34 AM6/13/19
to Ansible Project
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

Nicola Contu

unread,
Jun 14, 2019, 5:52:13 AM6/14/19
to ansible...@googlegroups.com
I found the way to loop and get variables.

Thanks a lot

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/51ca1b5c-84dc-6b52-8d5a-221b44725d1a%40b1-systems.de.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Jun 14, 2019, 1:11:26 PM6/14/19
to ansible...@googlegroups.com
On 14.06.2019 11:51, Nicola Contu wrote:
> I found the way to loop and get variables.

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.


--
Kai Stian Olstad

Angel Rengifo Cancino

unread,
Aug 16, 2019, 4:37:43 PM8/16/19
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages