skip to Main Content

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


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

    Login or Signup to reply.
  2. Use a loop instead
    Just add to the role:

        - name: PREREQUISITES INSTALL install-linux | Install rpm software Linux
      yum:
        name: "{{ item }}"
        state: installed
      loop:
        - "{{ software_packages }}"
    

    And to the defaults vars directory:

    software_packages:
              packages:
                - package: "hyperv-daemons"
                - package: "hypervkvpd"
                - package: "hyperv-tool"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search