Hi,
Something wrong with this ansible script :
hosts:
[test-server]
ip1 (for hostname1)
ip2 (for hostname2)
ip3 (for hostname3)
ip4 (for hostname4)
test.yml :
---
- hosts: all
become: yes
vars:
hostname:
- hostname1
- hostname2
- hostname3
- hostname4
- name: The following setting are changed
lineinfile:
path: /etc/rsyslog.d/test.conf
state: present
regexp: "{{ item.From }}"
line: "{{ item.To }}"
with_items:
- { From: '^.*target.*$', To: 'target="{{ item }}"' }
- { From: '^.*tls.myprivkey.*$', To: 'tls.myprivkey="/etc/pki/tls/private/{{ item }}.key"' }
- { From: '^.*tls.mycert.*$', To: 'tls.mycert="/etc/pki/tls/certs/{{ item }}.crt"' }
loop: "{{ hostname }}"
when: inventory_hostname in groups['test-server'] == hostname
Thanks for any help
You can use nested loops if the inner task is in a separate file, and you "include_tasks: inner_loop.yml".
You've also got a problem with both loops using "item" as a loop value - which "item" is which? To fix this, in the "loop:" section, have this, and use "{{ hostitem }}" when referring to the outer loop variable:
Open in new window
In fact, all of your problems disappear if you use "hosts: test_server" - you don't need the outer loop at all. Just use "{{ inventory_hostname }}" or "{{ ansible_hostname }}" to get the name of the host you are processing.