I think I found a way to do what I want to do. It's dirty but it works:

*--- # Copy hosts file*
*- hosts: all*
*  become: yes*
*  tasks:*
*  - name: update hosts template file*
*    copy:*
*      src: /home/ansible/hostfile *
*      dest: /tmp/hostfile*
*      owner: root*
*      group: root*
*      mode: 0644*
*      backup: yes*

*  - name: create master template file*
*    shell: diff /tmp/hostfile /etc/hosts | grep '< ' | sed 's/< //' > 
/tmp/masterhosts*

*  - name: Append template file to master host file*
*    shell: cat /tmp/masterhosts >> /etc/hosts*

*  - name: Delete temp file*
*    file:*
*      path: /tmp/hostfile*
*      state: absent*

*  - name: Delete master temp file*
*    file:*
*      path: /tmp/masterhosts*
*      state: absent*


On Monday, April 29, 2019 at 6:37:45 AM UTC-4, Jonathan Lozada De La Matta 
wrote:
>
> Hello,
>
> I think you should use templating and jinja2 a bit in here. Will make the 
> file easier to control and avoid appending. You can also use NetworkManager 
> (if you use that).
>
> On Mon, Apr 29, 2019 at 5:38 AM Kunalsing Thakur <[email protected] 
> <javascript:>> wrote:
>
>> Can you show me one example of your hosts file from one of node
>>
>> On Mon, 29 Apr 2019, 15:00 <[email protected] <javascript:> wrote:
>>
>>> I do not have a DNS server, hence the need to manually update the 
>>> /etc/hosts file. This a small network with less than 50 clients.
>>>
>>>
>>> On Monday, April 29, 2019 at 5:07:56 AM UTC-4, Kunalsing Thakur wrote:
>>>>
>>>> Why do you need that to append. If you have hosts file you can use copy 
>>>> to replace that role in every hosts file.  I assume you have DNS server
>>>>
>>>> On Mon, 29 Apr 2019, 14:28 <[email protected] wrote:
>>>>
>>>>> No, I'm not referring to the inventory file, it will be a separate 
>>>>> file that will have other hosts. However, you have pointed me in the 
>>>>> right 
>>>>> direction, thank you for that.
>>>>> Here is what I have come up with and this works, but my problem now is 
>>>>> that every time there are new servers added, the existing servers will 
>>>>> get 
>>>>> the same hosts records all the time so there will be duplicates in the 
>>>>> /etc/hosts. I haven't been able to figure out how to look at the existing 
>>>>> servers in the host file and only add new servers.:
>>>>>
>>>>> *--- # Copy hosts file*
>>>>> *- hosts: all*
>>>>> *  become: yes*
>>>>> *  tasks:*
>>>>> *  - name: update hosts template file*
>>>>> *    copy:*
>>>>> *      src: /home/ansible/hostfile *
>>>>> *      dest: /tmp/hostfile*
>>>>> *      owner: root*
>>>>> *      group: root*
>>>>> *      mode: 0644*
>>>>> *      backup: yes*
>>>>>
>>>>> *  - name: Append template file to master host file*
>>>>> *    shell: cat /tmp/hostfile >> /etc/hosts*
>>>>>
>>>>> *  - name: Delete temp file*
>>>>> *    file:*
>>>>> *      path: /tmp/hostfile*
>>>>> *      state: absent*
>>>>>
>>>>>
>>>>> This works, but my problem now is that every time there are new 
>>>>> servers added, the existing servers will get the same hosts records all 
>>>>> the 
>>>>> time so there will be duplicates in the /etc/hosts. I haven't been able 
>>>>> to 
>>>>> figure out how to look at the existing servers in the host file and only 
>>>>> add new servers.
>>>>>
>>>>>
>>>>>
>>>>> On Sunday, April 28, 2019 at 5:53:36 AM UTC-4, Kunalsing Thakur wrote:
>>>>>>
>>>>>> Yes. Use copy module. I hope so you are referring to inventory file 
>>>>>> which has all node details.
>>>>>> Like below
>>>>>>
>>>>>> /etc/ansible/inventory
>>>>>> [all]
>>>>>> host1
>>>>>> host2
>>>>>> host3
>>>>>>
>>>>>> /etc/ansible/playbook/master
>>>>>> host entry files
>>>>>>
>>>>>>
>>>>>> /etc/ansible/playbook/test.yaml
>>>>>>
>>>>>> host: all
>>>>>> tasks:
>>>>>>    - name: copying host file to all nodes
>>>>>>      copy:
>>>>>>          src: /etc/ansible/playbook/master
>>>>>>          dest: /etc/hosts
>>>>>>          mode: 0755
>>>>>>        
>>>>>>
>>>>>> On Sun, 28 Apr 2019, 15:16 <[email protected] wrote:
>>>>>>
>>>>>>> Actually, I am referring to the master file outside the playbook.
>>>>>>> In other words I just have a plain text master file with all my host 
>>>>>>> entries. Then, I create a playbook that references that master file to 
>>>>>>> send 
>>>>>>> to all nodes as reference to update their /etc/hosts file.
>>>>>>> Is that possible?
>>>>>>>
>>>>>>>
>>>>>>> On Sunday, April 28, 2019 at 5:43:06 AM UTC-4, Kunalsing Thakur 
>>>>>>> wrote:
>>>>>>>>
>>>>>>>> If I understand correctly the master file you are talking about is 
>>>>>>>> playbook term in ansible. The playbook has all information related to 
>>>>>>>> hosts 
>>>>>>>> and deployment instruction. You can give the Input in playbook and it 
>>>>>>>> will 
>>>>>>>> modify the stuff accordingly.
>>>>>>>>
>>>>>>>> On Sun, 28 Apr 2019, 15:09 <[email protected] wrote:
>>>>>>>>
>>>>>>>>> I hope I am posting this to the correct area...
>>>>>>>>>
>>>>>>>>> I'm really new to Ansible and am in the process of trying to learn 
>>>>>>>>> it.
>>>>>>>>> Question: Does Ansible have the ability to look at a master file 
>>>>>>>>> that contains host file information and deploy information from that 
>>>>>>>>> master 
>>>>>>>>> file to the /etc/hosts file on all the nodes in my environment so 
>>>>>>>>> that when 
>>>>>>>>> I add a new host all that I need to do is update that master file and 
>>>>>>>>> redeploy to all my nodes appending, or replacing, that existing 
>>>>>>>>> /etc/hosts 
>>>>>>>>> file?
>>>>>>>>>
>>>>>>>>> Thanks.
>>>>>>>>>
>>>>>>>>> -- 
>>>>>>>>> You received this message because you are subscribed to the Google 
>>>>>>>>> Groups "Ansible Project" group.
>>>>>>>>> To unsubscribe from this group and stop receiving emails from it, 
>>>>>>>>> send an email to [email protected].
>>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>>> To view this discussion on the web visit 
>>>>>>>>> https://groups.google.com/d/msgid/ansible-project/9a0cbe4c-98b9-4609-a3a8-843fbdbcd326%40googlegroups.com
>>>>>>>>>  
>>>>>>>>> <https://groups.google.com/d/msgid/ansible-project/9a0cbe4c-98b9-4609-a3a8-843fbdbcd326%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>>>> .
>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>
>>>>>>>> -- 
>>>>>>> You received this message because you are subscribed to the Google 
>>>>>>> Groups "Ansible Project" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it, 
>>>>>>> send an email to [email protected].
>>>>>>> To post to this group, send email to [email protected].
>>>>>>> To view this discussion on the web visit 
>>>>>>> https://groups.google.com/d/msgid/ansible-project/30e8efe2-7513-484b-bdaa-090c33e050f9%40googlegroups.com
>>>>>>>  
>>>>>>> <https://groups.google.com/d/msgid/ansible-project/30e8efe2-7513-484b-bdaa-090c33e050f9%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>> .
>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>
>>>>>> -- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "Ansible Project" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>>> an email to [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> To view this discussion on the web visit 
>>>>> https://groups.google.com/d/msgid/ansible-project/4094ac6f-b53c-42d9-b3ee-c405f2664cf3%40googlegroups.com
>>>>>  
>>>>> <https://groups.google.com/d/msgid/ansible-project/4094ac6f-b53c-42d9-b3ee-c405f2664cf3%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Ansible Project" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to [email protected] <javascript:>.
>>> To post to this group, send email to [email protected] 
>>> <javascript:>.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/ansible-project/a00f8ca8-cb4d-477a-a6a5-770bcfc6e939%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/ansible-project/a00f8ca8-cb4d-477a-a6a5-770bcfc6e939%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/CANErU6mKUvBL2Wb3bUPOu-Hh6nvq0BYMh8xc2gkfvJf6m3VP1A%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/ansible-project/CANErU6mKUvBL2Wb3bUPOu-Hh6nvq0BYMh8xc2gkfvJf6m3VP1A%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> -- 
>
> Jonathan lozada de la matta
>
> AUTOMATION PRACTICE
>
>
>  
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/f1ed186f-9540-4e7b-985a-cdb9fb472c63%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to