Portál AbcLinuxu, 4. května 2025 07:30

Dotaz: Ansible syntaxe with_items s proměnnými

12.12.2017 15:15 majales | skóre: 29 | blog: Majales
Ansible syntaxe with_items s proměnnými
Přečteno: 267×
Odpovědět | Admin
Ahoj
Mořím se ansible skriptem který má na konci přidávat hosta do invertory file, ale nefunguje mi to. Stěžuje si že nemá proměnnou.
  - name: Add hosts to local inventory file /etc/ansible/hosts
    local_action:
       module: lineinfile
       dest: /etc/ansible/hosts
       backup: yes
       regexp: "^{{host1}}"
       line: "{{ item.ansiblehost }} ansible_host={{ item.ansiblehostip }}"
       insertafter: '\[{{hostgroup}}\]'
       state: present
       with_items:
           - { ansiblehost: '{{host1}}' , ansiblehostip: '{{host1IP}}' }

Všechny tasky proběhnou, kromě posledního - přidání hosta od invertory file. Výsledek je takovýto:
TASK [Add hosts to local inventory file /etc/ansible/hosts] **************************************************************************************************************************************
fatal: [xxx]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'item' is undefined

The error appears to have been in '/etc/ansible/vmware_clone_pokus.yml': line 137, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:
 - name: Add hosts to local inventory file /etc/ansible/hosts\n    ^ here\n"}
	
Nástroje: Začni sledovat (0) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

12.12.2017 15:25 t
Rozbalit Rozbalit vše Re: Ansible syntaxe with_items s proměnnými
Odpovědět | | Sbalit | Link | Blokovat | Admin
Uber odsazení with_items.
12.12.2017 16:12 majales | skóre: 29 | blog: Majales
Rozbalit Rozbalit vše Re: Ansible syntaxe with_items s proměnnými
Odpovědět | | Sbalit | Link | Blokovat | Admin
Problém je v local_action .. nepodařilo se mi to zformátovat tak aby to bralo with_items. Nakonec zabralo toto:
  - name: Add hosts to local inventory file /etc/ansible/hosts
    lineinfile:
      dest: /etc/ansible/hosts
      backup: yes
      regexp: "^{{host1}}"
      line: "{{ item.ansiblehost }} ansible\_host={{ item.ansiblehostip }}"
      insertafter: '\[{{hostgroup}}\]'
      state: present
    with_items:
     - { ansiblehost: "{{host1}}", ansiblehostip: "{{host1IP}}" }
    delegate_to: localhost

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.