0

I have 3 vagrant boxes set up the following way:

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
  servers=[
      {
        :hostname => "machine1",
        :box => "ubuntu/trusty64",
        :ip => "192.168.77.21",
        :ssh_port => '2200'
      },
      {
        :hostname => "machine2",
        :box => "ubuntu/trusty64",
        :ip => "192.168.77.22",
        :ssh_port => '2201'
      },
      {
        :hostname => "machine3",
        :box => "ubuntu/trusty64",
        :ip => "192.168.77.23",
        :ssh_port => '2202'
      }
    ]

  script = <<-SCRIPT
    sudo apt update
    sudo apt install software-properties-common
    sudo add-apt-repository --yes --update ppa:ansible/ansible
    sudo apt -y install ansible
    ansible --version
  SCRIPT
  i=0
  servers.each do |machine|
      config.vm.define machine[:hostname] do |node|
          node.vm.box = machine[:box]
          node.vm.hostname = machine[:hostname]
          node.vm.network :private_network, ip: machine[:ip]
          node.vm.network "forwarded_port", guest: 22, host: machine[:ssh_port], id: "ssh"
          node.vm.synced_folder ".", "/home/vagrant/data"
          i += 1
          if i == servers.count
            # node.vm.provision "shell",
            #   inline: $script
            node.vm.provision "shell",
              inline: "sudo cp /home/vagrant/data/hosts /etc/ansible/hosts"
            node.vm.provision "shell",
              inline: "ansible-playbook /home/vagrant/data/playbook.yml -vv"
          end    
      end
  end
end

The playbook.yml tries to install java in order to instal ELK :

- hosts: all
  become: yes 
  tasks:

    - name: Update apt repo and cache 
      apt:
        update_cache: yes
        force_apt_get: yes
        cache_valid_time: 3600

    - name: Add Oracle Java Repository
      apt_repository: repo='ppa:webupd8team/java'

    - name: Accept Java 8 License
      debconf: name='oracle-java8-installer' question='shared/accepted-oracle-license-v1-1' value='true' vtype='select'

    - name: Install Oracle Java 8
      apt:
       name: openjdk-8-jdk
       state: present
       update_cache: yes


    - name: Add apt key
      shell: curl -s https://artifacts.elastic.co/GPG-KEY-elasticsearch | apt-key add -

    - name: Adding Elasticsearch repo
      apt_repository:
       repo: deb https://artifacts.elastic.co/packages/6.x/apt stable main
       state: present
       filename: elastic-6.x.list
       update_cache: yes

    # Installing Elasticsearch
    - name: Install Elasticsearch
      apt:
       name: elasticsearch
       state: latest
       update_cache: yes

The playbook fails in Install Oracle Java 8 step:

The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
  File "/tmp/ansible_apt_payload_vC9e2b/ansible_apt_payload.zip/ansible/modules/packaging/os/apt.py", line 431, in package_status
  File "/usr/lib/python2.7/dist-packages/apt/cache.py", line 238, in __getitem__
    raise KeyError('The cache has no package named %r' % key)
fatal: [192.168.77.22]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "allow_unauthenticated": false,
            "autoclean": false,
            "autoremove": false,
            "cache_valid_time": 0,
            "deb": null,
            "default_release": null,
            "dpkg_options": "force-confdef,force-confold",
            "force": false,
            "force_apt_get": false,
            "install_recommends": null,
            "name": "openjdk-8-jdk",
            "only_upgrade": false,
            "package": [
                "openjdk-8-jdk"
            ],
            "policy_rc_d": null,
            "purge": false,
            "state": "present",
            "update_cache": true,
            "upgrade": null
        }
    },
    "msg": "No package matching 'openjdk-8-jdk' is available"

When trying to install java, in anyway (via the playbook or terminal) getting:

sudo apt-get install -y openjdk-8-jdk
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package openjdk-8-jdk

How can I install java in the vagrant box and proceed to install ELK?

leandrojmp
  • 7,082
  • 2
  • 19
  • 24
Yael
  • 25
  • 1
  • 8
  • According your the given message `raise KeyError`, as well the next task about "_Add apt key_", does [Download and add apt key](https://stackoverflow.com/a/70323221/6771046) help to resolve your issue? You need to add a key for Oracle repository too. – U880D Mar 19 '22 at 15:17
  • @U880D I added curl -s https://yum.oracle.com/RPM-GPG-KEY-oracle-ol8 | apt-key add - step but still same error. – Yael Mar 19 '22 at 16:59
  • How about adding the key by using Ansible modules? Like `apt_key: url: https://yum.oracle.com/RPM-GPG-KEY-oracle-ol8 state: present`. – U880D Mar 19 '22 at 17:04

0 Answers0