1

I have task to read data from csv file and push result to templates and copy those templates to different servers. however, i am getting error while writing to template. below are details -

main.yml

- name: Print return information from the previous task
  vars:
    test_csv: "{{ lookup('file', '/u00/ansible/Playbooks/files/newrelic_test.csv', wantlist=True) }}"
  ansible.builtin.debug:
    var: test_csv

- name: copy template
  template:
    src: /u00/ansible/Playbooks/files/infra-config.yml_template
    dest: /u00/app/monitor/infra-config.yml
  with_items: test_csv
  notify: confirm copy done

- name: Start the New Relic Service
  ansible.builtin.systemd:
    name: infra.service
    state: started
  become: yes
  become_user: root

infra-config.yml_template -

custom_attributes:
    application : {{ item.Application }}
    env : {{ item.env }}
    datacenter : {{ item.Datacenter }}
log:
 file: /u00/app/monitor/infra.log

csv file content

Application,Host,env,Datacenter
Microsoft,testserver1,TEST,DC1
Apple,testserver2,TEST,DC2

error -

> TASK [config-update : copy template]
> ******************************************* [0;31mAn exception occurred during task execution. To see the full traceback, use -vvv.
> The error was: ansible.errors.AnsibleUndefinedVariable:
> 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute
> 'db_name'[0m [0;31mfailed: [testserver1]
> (item=test_csv) => {"ansible_loop_var": "item", "changed": false,
> "item": "test_csv", "msg": "AnsibleUndefinedVariable:
> 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute
> 'db_name'"}

Expectation is to read csv file and use variables in template in different servers.

testserver1 -

> custom_attributes: application : Microsoft env : Test datacenter : DC1
> log: file: /u00/app/monitor/infra.log

testserver2 - 

> custom_attributes: application : Apple env : Test datacenter : DC1
> log: file: /u00/app/monitor/infra.log

ayuuk ja'ay
  • 382
  • 1
  • 3
  • 13

1 Answers1

0

There are few things to fix in your playbook. First, you are defining your test_csv variable inside a task and it cannot be accessible by other tasks. You can use register instead. However, the first task returns a list but with one string like this "test_csv": ["Application,Host,env,Datacenter\nMicrosoft,testserver1,TEST,DC1 \nApple,testserver2,TEST,DC2"] which only results one item in the test_csv list.

You can achieve this by using read_csv module as well. Below I demonstrate how:

Note that I have added a condition using inventory_hostname on the copy template task since you might want to target each csv line according to its hostname. You can modify this according to your needs.

Csv file content:

Application,Host,env,Datacenter
Microsoft,localhost,TEST,DC1 
Apple,testserver2,TEST,DC2

Example of playbook for testing:

- name: Check status
  hosts: localhost
  gather_facts: no
  tasks:
    - name: read csv file and return a list
      read_csv:
        path: test.csv
      register: applications
    
    - name: Ouput applications from previous task
      debug:
        msg: "{{ item.Application }}"
      loop: "{{ applications.list }}"
        
    - name: copy template
      template:
        src: src.yml_template ##I would recommendr to use .j2 jinja template instead.
        dest: dest.yml
      loop: "{{ applications.list }}"
      when: inventory_hostname == item.Host

src.yml_template content:

custom_attributes:
    application : {{ item.Application }}
    env : {{ item.env }}
    datacenter : {{ item.Datacenter }}

log:
 file: /u00/app/monitor/infra.log

Gives in dest.yml:

custom_attributes:
    application : Microsoft
    env : TEST
    datacenter : DC1 

log:
 file: /u00/app/monitor/infra.log

Cli output:

PLAY [Check status] **********************************************************************************************************************************************************

TASK [read csv file and return a list] ***************************************************************************************************************************************
ok: [localhost]

TASK [Ouput applications from previous task] *********************************************************************************************************************************
ok: [localhost] => (item={'Application': 'Microsoft', 'Host': 'localhost', 'env': 'TEST', 'Datacenter': 'DC1 '}) => {
    "msg": "Microsoft"
}
ok: [localhost] => (item={'Application': 'Apple', 'Host': 'testserver2', 'env': 'TEST', 'Datacenter': 'DC2'}) => {
    "msg": "Apple"
}

TASK [copy template] *********************************************************************************************************************************************************
changed: [localhost] => (item={'Application': 'Microsoft', 'Host': 'localhost', 'env': 'TEST', 'Datacenter': 'DC1 '})
skipping: [localhost] => (item={'Application': 'Apple', 'Host': 'testserver2', 'env': 'TEST', 'Datacenter': 'DC2'}) 
ayuuk ja'ay
  • 382
  • 1
  • 3
  • 13