[ILUGC] Re: Passing shell variable to extra variable

  • From: Ishwaryaa Lakshmi <ishwaryalakshmi.c@xxxxxxxxx>
  • To: ilugc@xxxxxxxxxxxxx
  • Date: Tue, 7 Jul 2020 04:31:29 +0530

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,
I’ve written an ansible code for installing list of packages in multiple
servers and Invoking it using shell script. I used ansible distribution
to
execute commands based on os type.

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
to
pass this variable in extra vars for ansible.

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
code.

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
please
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/



_____________________________________
ILUGC List: //www.freelists.org/list/ilugc
ILUGC Web: http://ilugc.in/

Other related posts: