Skip to content
Advertisement

Install packages from a list through ansible

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

Advertisement

Answer

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 use lookup to get file contents.

Example:

    # Run the awk command to get only package names
    - command: awk '{print $1}' path/to/packages.txt
      register: _pkgs
      delegate_to: localhost
      run_once: true
    
    - name: install packages
      yum:
        - name: "{{ _pkgs.stdout_lines }}"
          state: present

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 with awk (above example updated), and use it for package install.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement