Dictionary Parsing

11 views
Skip to first unread message

Mona Gopal

unread,
Feb 13, 2018, 5:17:34 AM2/13/18
to Ansible Project

We are trying to write Ansible playbooks for releasing patches for an application

In patch releases there are 4 basic changes that can be there

We will create 4 roles for those

BinaryRole : For changing war files and jar files with backup and replacing the existing configurations

ConfigRole: For adding / deleting / modifying parameters from the configuration files with backup

DBRole : For changes related to DB . SQL source file will be used

NonCompile: For replacing static files like JSPs or Resource_Properties

 

While working on the ConfigRole I am using the dictionary variables for defining files and parameters will require change in those files

The parameters can be either added or modified or removed from the config file

Below is the dictionary variable that I have defined and the corresponding task that I have used that variable in

 

Inside group_vars file

 

configFiles:

    /home/BM/apache-tomcat-8.0.35/webapps/OBDReport/WEB-INF/classes/COMMON.properties:

           paramName: DB_USER

           paramValue: dbansible

           change: 3

 

Inside playbook in ConfigRole

 

- name: change configurations modified

  lineinfile: dest={{ item.key }} regexp='^{{ item.value.paramName }}=' line={{ item.value.paramName }}={{ item.value.paramValue }}

  when: "{{ item.value.change }}|int==3"

  with_dict: "{{ configFiles | default({}) }}"

 

- name: change configurations added

  lineinfile: dest={{ item.key }} line={{ item.value.paramName }}={{ item.value.paramValue }}

  when: "{{ item.value.change }}|int==2"

  with_dict: "{{ configFiles | default({}) }}"

 

In the above task based on the change value parameters will be modified for value 3 and added for value 2

While looping over the dictionary configFiles:   ansible is able to read only one value of every key

Here key is /home/BM/apache-tomcat-8.0.35/webapps/OBDReport/WEB-INF/classes/COMMON.properties and value is the set of values (paramName , paramValue, change )

If there are multiple parameters in the same file which require change then how do we define those in the group_vars file and how to loop through the same in the task file in the role

 

If I write the vars as below

configFiles:

    /home/BM/apache-tomcat-8.0.35/webapps/OBDReport/WEB-INF/classes/COMMON.properties:

           paramName: DB_USER

           paramValue: dbansible

           change: 3

           paramName: DB_USER_1

           paramValue: dbansible1

           change: 2

Then it will only read the last set of values for each key.

Please help in resolving this or suggest if we can use some other approach


Thanks in advance,

Mona G

Reply all
Reply to author
Forward
0 new messages