I have 2 servers named testserver and vsphere server. Ansible is installed on testserver and I have a list of packages stored in a text file packages.txt. How can I install those packages on vsphere server through ansible? This is my packages.txt file. I just have to fetch the package name from the packages.txt
hyperv-daemons| x86_64
hypervkvpd |x86_64
hyperv-tool | x86_64
nginx | x84_64
And this the yml file inside testserver
- hosts: vsphere
tasks:
- name: Install the packages
shell: sh /root/test.sh
Test.sh contains
#/bin/bash
for i in `cat /root/packages`
do
echo $i
yum install $i
done
When I run this ansible script, I am getting an error: no packages.txt is found on vsphere. Any help would be appreciated
2
Answers
Firstly I would suggest that you use the yum module to install the packages. We can pass a list of packages to install by reading the
packages.txt
file. Seems this file is on the Ansible machine, so we can uselookup
to get file contents.Example:
Edit
If you have
| x86_64
in the file for every package, it wouldn’t work with your shell script either. The easiest way would be get the clean list of packages withawk
(above example updated), and use it for package install.Use a loop instead
Just add to the role:
And to the defaults vars directory: