In this question, I firstly thought about creating three different plays.
---
- name: dev
hosts: dev
tasks:......
- name: test
hosts: test
tasks:......
- name: prod
hosts: prod
tasks:......
but then found out example answer used when: 'inventory_hostname in groups.(dev/prod/test)'
I think the result will be identical, but it's a bit tricky as it said to run all hosts.. is the way I've done alright?
One more question is, I used lineinfile: line= path= but the answer used copy: content= dest=
I noticed that when I used lineinfile without regexp, the line=' ' will be just added at the end of line. Otherwise, content= of copy: is basically replacing whole thing inside of /etc/issue.
Since these issues.. this question seems very very tricky for me.
I hope actual exam would be more explicitly explained
@spurs I am not sure from where you got this question and if it is appropriate for this community to comment or provide solution on that - it may be a copyright material or some exam questions from external providers.
Based on your comments, even when you write three individual tasks for individual hosts - it wont run and configure each task on all hosts - so by your logic too the 3 tasks will be conditionally applied to separate hosts - so it is fine.
Having said that - from the question it does appear to me that it wants one to use conditionals in a single task to achieve the desired configuration because the playbook should run on all hosts and then filter the hosts based on condition. Hence, the solution looks more correct in the given context.
Regarding lineinfile vs copy :
lineinfile --> This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression.
This is primarily useful when you want to change a single line in a file only.
copy --> The ansible.builtin.copy module copies a file or a directory structure from the local or remote machine to a location on the remote machine. File system meta-information (permissions, ownership, etc.) may be set, even when the file or directory already exists on the target system.
Hence, I would prefer copy in the given scenario.
@spurs I am not sure from where you got this question and if it is appropriate for this community to comment or provide solution on that - it may be a copyright material or some exam questions from external providers.
Based on your comments, even when you write three individual tasks for individual hosts - it wont run and configure each task on all hosts - so by your logic too the 3 tasks will be conditionally applied to separate hosts - so it is fine.
Having said that - from the question it does appear to me that it wants one to use conditionals in a single task to achieve the desired configuration because the playbook should run on all hosts and then filter the hosts based on condition. Hence, the solution looks more correct in the given context.
Regarding lineinfile vs copy :
lineinfile --> This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression.
This is primarily useful when you want to change a single line in a file only.
copy --> The ansible.builtin.copy module copies a file or a directory structure from the local or remote machine to a location on the remote machine. File system meta-information (permissions, ownership, etc.) may be set, even when the file or directory already exists on the target system.
Hence, I would prefer copy in the given scenario.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.