Add Hosts for Ansible

System Env

[root@ansible ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@ansible ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core
[root@ansible ~]# ansible --version
ansible 2.7.7
 config file = None
 configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
 ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
 executable location = /usr/local/bin/ansible
 python version = 3.6.6 (default, Jan 26 2019, 16:53:05) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

Generate Ansible Server's public key

[root@ansible ~]# ssh-keygen

Deploy Ansible Server's public key

  • Deploy public key to s1
    [root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@s1
    

Add Hosts for Ansible

[root@ansible ~]# mkdir /etc/ansible
[root@ansible ~]# vim /etc/ansible/hosts
[root@ansible ~]# cat /etc/ansible/hosts
[servers]
s1

Run some test command

[root@ansible ~]# ansible s1 -m ping
s1 | SUCCESS => {
 "changed": false,
 "ping": "pong"
}

How To Install Ansible

System Env

  • OS info
    [root@ansible /]# cat /etc/redhat-release
    CentOS Linux release 7.6.1810 (Core)
    [root@ansible /]# lsb_release -a
    LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
    Distributor ID: CentOS
    Description: CentOS Linux release 7.6.1810 (Core)
    Release: 7.6.1810
    Codename: Core
    

Install Python36

  • upgrade system pkgs
    [root@ansible /]# yum clean all && yum makecache
    [root@ansible /]# yum upgrade -y
    
  • install python36
    [root@ansible /]# yum install epel-release -y
    [root@ansible /]# yum clean all && yum makecache
    [root@ansible /]# yum install python36 python36-devel python36-pip -y
    
  • set pip3.6 as the default pip
    [root@ansible /]# pip3.6 install --upgrade pip
    

Install Ansible

[root@ansible /]# pip install ansible

Run it in Docker

  • write a Dockerfile
    [root@DockerServer /]# cat Dockerfile
    FROM centos:latest
    MAINTAINER Kyle Chen
    ENV LANG C.UTF-8
    ENV DEBIAN_FRONTEND=noninteractive
    RUN yum clean all && \
    yum makecache && \
    yum upgrade -y && \
    yum install vim openssh-server epel-release -y && \
    yum clean all && \
    yum makecache && \
    yum install python36 python36-devel python36-pip -y && \
    echo "set -o vi" >> /etc/bashrc && \
    pip3.6 install --upgrade pip && \
    pip install ansible && \
    echo "PASSWORD" | passwd --stdin USER && \
    systemctl enable sshd
    
  • build an image
    [root@DockerServer /]# docker build . -t ansible
    
  • run image
    [root@DockerServer /]# docker run --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro --ip IP --dns DNS --name ansible --hostname ansible -tdi ansible:latest /usr/sbin/init
    
  • ATTENTION:
    You must set the PASSWORD, USER, IP, DNS which can be found before you run image. After run image, you can use 'ssh USER@IP' and input password to login on the server.