Re: [ansible-project] update hots file - the default inventory file

20 views
Skip to first unread message

Abhijeet Kasurde

unread,
Mar 2, 2021, 11:53:17 PM3/2/21
to ansible...@googlegroups.com
Can you provide output of lineinfile ? Mine look like

changed: [localhost] => (item=a) => {"ansible_loop_var": "item", "backup": "", "changed": true, "item": "a", "msg": "line added"}
changed: [localhost] => (item=b) => {"ansible_loop_var": "item", "backup": "", "changed": true, "item": "b", "msg": "line added"}

On Wed, Mar 3, 2021 at 10:14 AM esxi...@gmail.com <esxi...@gmail.com> wrote:
Hi

I have a playbook which create the vm in vCenter
---
- hosts: localhost
  gather_facts: false
  connection: local
  vars_files:
    - ./vars/xxx.yml

  tasks:


    - name: Create VM using template
      vmware_guest:
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        datacenter: "{{ vcenter_datacenter }}"
        cluster: "{{ vcenter_cluster }}"
        template: "{{ vcenter_template }}"
  ..
          hostname: "{{ item }}"
      with_items: "{{ servers }}"




    - name: Update the Inventory
      lineinfile:
        dest: /etc/ansible/hosts
        insertafter: '^\[MyServers\]'
        line: '{{ item }}'
      with_items: "{{ servers }}"
      run_once: true



VM create works well, Any suggestions why the the hosts file is not getting updated .. above is the code

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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/542ded49-eab9-4fbf-8faa-2ba91bc3517en%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

esxi...@gmail.com

unread,
Mar 3, 2021, 2:08:50 AM3/3/21
to Ansible Project
Thanks for  the reply Abhi

What i noticed is it does update the file if there is no entry like blow is the o/p


TASK [Update the Inventory] *********************************************************************************************************************************************************
changed: [localhost] => (item=xx)


but if the entry already present in more than 1 group say .. then it does not update the file & the output is

TASK [Update the Inventory] *********************************************************************************************************************************************************
ok: [localhost] => (item=xxx)



So if a fresh box it is ok ( ie never had been provisioned  )

esxi...@gmail.com

unread,
Mar 3, 2021, 2:11:27 AM3/3/21
to Ansible Project
for eg



[group1]
server1
server2
[group2]
server1
server2


if you remove from group one & run does not update .... So lineinfile should have someway to say the position exactly below [group1] add is missing ...

Vladimir Botka

unread,
Mar 3, 2021, 4:01:59 AM3/3/21
to esxi...@gmail.com, ansible...@googlegroups.com
On Tue, 2 Mar 2021 23:08:50 -0800 (PST)
"esxi...@gmail.com" <esxi...@gmail.com> wrote:

> but if the entry already present in more than 1 group say .. then it does
> not update the file

Try "ini_file", e.g.
https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html

- ini_file:
dest: /etc/ansible/hosts
section: MyServers
option: "{{ item }}"
allow_no_value: true
loop: "{{ servers }}"

--
Vladimir Botka

esxi...@gmail.com

unread,
Mar 4, 2021, 12:18:20 AM3/4/21
to Ansible Project
Thanks Vladimir Botka 
Reply all
Reply to author
Forward
0 new messages