cancel
Showing results for 
Search instead for 
Did you mean: 
spurs
Flight Engineer
Flight Engineer
  • 181 Views

Question about inventory_hostname in groups.something, copy/lineinfile to replace contexts

Jump to solution

In this question, I firstly thought about creating three different plays.

spurs_0-1712802205581.png

---

- 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)'

spurs_1-1712802298029.png

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? 

spurs_2-1712802391848.png

 

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

 

 

Labels (1)
1 Solution

Accepted Solutions
Chetan_Tiwary_
Moderator
Moderator
  • 112 Views

@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. 

View solution in original post

1 Reply
Chetan_Tiwary_
Moderator
Moderator
  • 113 Views

@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. 

Join the discussion
You must log in to join this conversation.