Reputation: 641
I'm trying to replace name in file based on the artifacts that ansible has collected.
It's there some simple way to achieve this task with ONE lineinfile
Something like this,
lineinfile:
path: '/somefile.conf'
regexp: '{{ item.regexp }}'
line: '{{ item.line }}'
with_items:
- { regexp: '^DB_TYPE=', line: "DB_TYPE=mysql" } when: mysql.stat.exists
- { regexp: '^DB_TYPE=', line: "DB_TYPE=oracle" } when: oracle.stat.exists
Upvotes: 1
Views: 228
Reputation: 18270
This can be done with if ... else ... if
lineinfile:
path: '/somefile.conf'
regexp: '^DB_TYPE='
line: "{{ 'DB_TYPE=mysql' if mysql.stat.exists else 'DB_TYPE=oracle' if oracle.stat.exists }}"
Upvotes: 2
Reputation: 409
I believe when
conditions are tied to a task, and thus you cannot have different conditions for different loops in the same task (happy to be corrected here).
This leaves you a couple options:
lineinfile
tasks, which it sounds like you want to avoid.set_fact
module. Not necessarily better, but cool to be aware of.An example of the second option:
- name: set database type for mysql deployment
set_fact:
_db_type: mysql
when: mysql.stat.exists
- name: set database type for oracle deployment
set_fact:
_db_type: oracle
when: oracle.stat.exists
- name: something something
lineinfile:
path: '/somefile.conf'
regexp: '^DB_TYPE='
line: "DB_TYPE={{ _db_type }}"
I find this helpful if there are multiple tasks where you'll need to refer to either mysql or oracle (as an example).
Upvotes: 0