Reputation: 35
Can we use multiple with_items
in same task, in this example I'm using two with_items
, the first one is to store values like net.ipv6.conf.all.disable_ipv6 = 1
and the second one is to get the data captured in the register variable data_captured
Using this way I have conflicts.
- name: Test with with_items
block:
- name: Searching several string
command: grep -w "{{item}}" /root/test/sysctl.conf
register: data_captured
with_items:
- 'net.ipv6.conf.all.disable_ipv6 = 1'
- 'net.ipv6.conf.default.disable_ipv6 = 1'
- 'net.ipv6.conf.lo.disable_ipv6 = 1'
- 'vm.swappiness = 1'
- assert:
that:
- "'net.ipv6.conf.all.disable_ipv6 = 1' in item.stdout"
- "'net.ipv6.conf.default.disable_ipv6 = 1' in item.stdout"
- "'net.ipv6.conf.lo.disable_ipv6 = 1' in item.stdout"
- "'vm.swappiness = 1' in item.stdout"
success_msg: "Protocols are defined in the file"
with_item: '{{ data_captured.results }}'
rescue:
- name: Insert/Update /root/test/sysctl.conf
blockinfile:
path: /root/test/sysctl.conf
block: |
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
vm.swappiness = 1
Upvotes: 1
Views: 4211
Reputation: 4554
You should use the sysctl module instead of working around it like that.
This will do what you want:
- name: ensure values are set correctly in sysctl
ansible.posix.sysctl:
name: '{{ item.name }}'
value: '{{ item.value }}'
state: present
reload: yes
loop:
- name: 'net.ipv6.conf.all.disable_ipv6'
value: '1'
- name: 'net.ipv6.conf.default.disable_ipv6'
value: '1'
- name: 'net.ipv6.conf.lo.disable_ipv6'
value: '1'
- name: 'vm.swappiness'
value: '1'
You might need to run ansible-galaxy collection install ansible.posix
on your ansible-controller (where you run the ansible
or ansible-playbook
command) before running the playbook.
Additional notes:
with_*
is discouraged, loop
should be used instead (see docs)sysctl
module did not existwith_item
on the assert
is completely superfluous in your case, as you do not use {{ item }}
in the taskUpvotes: 3