Skip to content

koheitakumi/ansible-docker-ubuntu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ansible-docker-ubuntu

This is a sample template to use ansible with docker. The ansible server and inventries are ubuntsu(bionic = 18.04).

We prepare ansible server and inventries by docker-compose command. After connecting ansible server, execute ansible command. This command just do download README.md of this repository. Really simple😃

How to use it.

Preparation

  1. Clone this project in your PC.

    git clone https://github.com/koheitakumi/ansible-docker-ubuntu.git
  2. Install Docker.

  3. Generate a SSH key pair.

    • The secret key is for the ansible server to access inventries.
    • The public key is for the inventries to accept the ansible server.
    1. Generate key pair for this sample.
      ssh-keygen -t ed25519 -N "" -f ~/.ssh/test_id_rsa
      If you're machine is windows, please check this article. I could recommend you to use Git Bash because I suppose you have already installed it to use git.
    2. Copy the content to the file.
      • Secret key: Copy the content of test_id_rsa and paste it to docker/ansible/id_rsa
      • Public key: Copy the content of test_id_rsa.pub and paste it to docker/target/id_rsa.pub

Build and run

  1. Build containers by docker-compose.
    docker-compose up -d
  2. Check the containers which have just built.
    docker ps
    # result of `docker ps`
    
    CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS               NAMES
    d3a99ef8ab16        ansible-docker-ubuntu_target01   "/usr/sbin/sshd -D"      20 minutes ago      Up 20 minutes       22/tcp              target01
    aee0ebbba82e        ansible-docker-ubuntu_ansible    "/bin/sh -c /bin/bash"   20 minutes ago      Up 20 minutes                           ansible
    dadfcdc95fa9        ansible-docker-ubuntu_target02   "/usr/sbin/sshd -D"      20 minutes ago      Up 20 minutes       22/tcp              target02
  3. Connect the ansible server.
    docker exec -it ansible sh
  4. Confirm that you can connect target01 and 02 server from ansible server.
    # At first, please type `yes`, when you are confirmed to connect with ssh.
    ssh target01
    
    # After connect target01
    exit
    ssh target02
    
    exit
  5. Execute ansible command to the target01 and 02.
    # On the ansible server.
    ansible-playbook -i inventry.ini playbook.yml
    # You can find the following message on success.
    
    TASK [Test] ********************************************************************************************************************************************************
    [WARNING]: Consider using the get_url or uri module rather than running 'wget'.  If you need to use command because get_url or uri is insufficient you can add
    'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
    changed: [target01]
    changed: [target02]
    
    PLAY RECAP *********************************************************************************************************************************************************
    target01                   : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    target02                   : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  6. Check whether the README.md file exists on each target01 and 02.
    ssh target01
    
    ls
    # README.md
    
    exit
    ssh target02
    
    ls
    # README.md
    
    exit

Clean up

  1. Disconnect ansible server.
    exit
  2. On the host, stop containers.
    docker-compose down
    # result
    
    Stopping target01 ... done
    Stopping ansible  ... done
    Stopping target02 ... done
    Removing target01 ... done
    Removing ansible  ... done
    Removing target02 ... done
    Removing network ansible-docker-ubuntu_default
  3. Check the status of containers if necessary.
    docker ps -a
  4. Remove all images on this project
    docker rmi ansible-docker-ubuntu_ansible ansible-docker-ubuntu_target01 ansible-docker-ubuntu_target02
  5. Check the images if necessary.
    docker images

Enjoy ansible with docker!

About

An ansible template for docker on ansible based Ubuntu

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published