Score:0

How to create a SSH connection between Ansible and Jenkins containers

uz flag

I'm new to Jenkins.

My goal is to create a Jenkins job\pipeline that will activate a group of ansible's playbook files - which will create a long installation of my tested product.

I build 2 containers in Docker desktop (Windows): Ansible and Jenkins.

Below Ansible docker-compose.yml file:

version: '2'
services:
  ansible:
    container_name: ansible
    hostname: ansible
    image: ansible
    volumes:
      - ~/product/ansible:/ansible
    working_dir: /ansible
    build:
      context: .
      dockerfile: Dockerfile
    dns:
      - 200.20.0.20
    networks:
    - network

networks:
  network:
    external: true

Below Jenkins docker-compose.yml file:

version: '3.7'
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins:lts
    privileged: true
    user: root
    ports:
      - 8080:8080
      - 50000:50000
    container_name: jenkins
    volumes:
    - ~/r10_core/jenkins:/jenkins
    networks:
    - network

networks:
  network:
    external: true

Ansible and Jenkins are on the same Network Id. So, ping command is working from Ansible to Jenkins and vis versa. Both containers have access to Internet.

I set username and password admin | admin and installed the following plugins in Jenkins container through Jenkins site > Manage Jenkins > Manage Plugins:

  • Ansible plugin
  • SSH
  • Publish by SSH

I checked the SSH services in both machines.

Ansible:

[root@ansible ansible]# ssh 172.20.0.2
ssh: connect to host 172.20.0.2 port 22: Connection refused
[root@ansible ansible]# service --status-all
bash: service: command not found
[root@ansible ansible]# systemd
bash: systemd: command not found
[root@ansible ansible]# systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
[root@ansible ansible]#

Jenkins:

root@jenkins:/# ssh 172.20.0.3
ssh: connect to host 172.20.0.3 port 22: Connection refused
root@jenkins:/# service --status-all
 [ ? ]  hwclock.sh
root@jenkins:/#

I got a Connection refused also when trying to connect to the machines themselves

a) What should I configure in the Ansible container in order SSH to be active? b) What should I configure in the Jenkins container in order SSH to be active? c) What should I configure in each container in order them talk each other through SSH (i.e port 22)?

RVT avatar
it flag
RVT
I think this question should be rephrased. It's not clear what you are actually *trying to do*, here. "Separate Containers" is **one** problem, while creating or autogenerating an `ansible-playbook` or `ansible` command is very different. (TLDR; Jobs may also intra-communicate to some extent - more with "a bit of imagination")
uz flag
@RVT thanks for you response. I focused the question to one problem - SSH connection between Ansible and Jenkins containers
in flag
You misunderstood the concept of containers. What you want are VMs.
uz flag
@GeraldSchneider The principle in containers is that each container performs only one service and in a good way. I want the Jenkins service to consume an Ansible service. Is it possible? If so, you can indicate this in response to the post, if not - please clarify your position as to why such a definition is not possible. Thanks.
RVT avatar
it flag
RVT
@Hiddai... there's no such thing as a "Jenkins container," unless you're talking about containers managed by Jenkins, under Docker or another container provider. SSH connections between one host and another are a pretty well understood problem (hint: look at ssh-agent and ssh keys)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.