I passed the text file as extra variable as mentioned and it worked. Thank
you.
Thanks and regards
*Ishwarya Lakshmi. C*
On Tue, Jul 7, 2020 at 6:49 AM Mohan L <l.mohanphy@xxxxxxxxx> wrote:
On Mon, Jul 6, 2020 at 11:40 PM Ishwaryaa Lakshmi <
ishwaryalakshmi.c@xxxxxxxxx> wrote:
Hello everyone,to
Iâve written an ansible code for installing list of packages in multiple
servers and Invoking it using shell script. I used ansible distribution
execute commands based on os type.to
The package list is a text file with 10+ package names in it. Iâve
converted this text file into array using âmapfileâ in shell and trying
pass this variable in extra vars for ansible.code.
script.sh
mapfile -t myvar < /path/file.txt
ansible-playbook playname.yml -e package = ${myvar[@]}
The packages are looped using âwith_itemsâ to install in the ansible
please
The problem Iâm facing is while passing the extra variable the list of
packages are not getting installed only the first one is installed.
How can we pass the list of files to ansible extra variable. Anyone
suggest how to fix this.
May be you have to pass file path in extra variable and use file lookup
plugin to install packages. Here is the example.
# cat /tmp/packages.txt
zip
wget
git
nginx
vim
# cat /etc/ansible/hosts
localhost
# cat /etc/ansible/playbook.yml
---
- hosts: all
remote_user: root
tasks:
- debug:
msg: "{{item}}"
loop: "{{ lookup('file', package_list).splitlines() }}"
- name: Install list of packages
apt:
name: "{{item}}"
state: present
loop: "{{ lookup('file', package_list).splitlines() }}"
Invoke the playbook like below in your script.
# ansible-playbook -i hosts playbook.yml -e
"package_list=/tmp/packages.txt"
PLAY [all]
*********************************************************************
TASK [Gathering Facts]
*********************************************************
ok: [localhost]
TASK [debug]
*******************************************************************
ok: [localhost] => (item=zip) => {
"msg": "zip"
}
ok: [localhost] => (item=wget) => {
"msg": "wget"
}
ok: [localhost] => (item=git) => {
"msg": "git"
}
ok: [localhost] => (item=nginx) => {
"msg": "nginx"
}
ok: [localhost] => (item=vim) => {
"msg": "vim"
}
TASK [Install list of packages]
************************************************
ok: [localhost] => (item=zip)
ok: [localhost] => (item=wget)
changed: [localhost] => (item=git)
changed: [localhost] => (item=nginx)
ok: [localhost] => (item=vim)
PLAY RECAP
*********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
https://pastebin.com/dxs5gYwH
Thanks
Mohan
_____________________________________
ILUGC List: //www.freelists.org/list/ilugc
ILUGC Web: http://ilugc.in/