DevOps
  • DevOps
  • Docker
    • Overview
  • Vagrant
    • Overview
    • Installation
    • Basic Commands
    • Vagrantfile
      • Example
    • Deploying Multiple Machine
    • Distribution
    • Reference
  • Running SQL Server on Windows Docker
    • Introduction
      • Windows Container & Hyper-V Container
    • System Requirements and Prerequisites
    • Version Compatibility
    • Enable Hyper-V & Containers and Install Docker on Window Servers
    • Using Docker to Run MSSQL Image
    • Install SSMS for remoting MSSQL Server Image
    • Using Docker-Compose to Build Up a MSSQL Image
    • Increase the default Windows container's storage
    • Reference
    • Appendix
  • Kubernetes
    • Install Kubernetes with kubeasz
    • Use Hyper-V Manager to create VM for K8S
      • Specification of the VM
      • Use Hyper-V Manager to turn on a VM for K8S
    • Installation
      • Install VirtualBox and its extension on Ubuntu
      • Install kubectl
      • Install minikube
    • Getting Started
      • Running Kubernetes Locally via Minikube
    • Assign Memory Resources to Containers and Pods
    • Assign CPU Resources to Containers and Pods
    • Quality of Services (QoS)
    • Volume for Storage
      • Configure a Pod to Use a Volume for Storage
      • *Configure a Pod to Use a PersistentVolume for Storage
    • In a nutshell...
    • Reference
  • Ansible
    • What is Ansible?
    • Set Up OS Environments for Ansible
    • Install Ansible
    • Structure for ansible
      • Inventory
      • Playbook
      • Modules
      • Variables
      • Conditions
    • Security - Ansible Fault
    • 實際操作測試1 - 使用ssh
    • 實際操作測試2 - Docker容器部署
    • Appendix
    • Reference
  • Azure
    • Overview
    • Terms
    • Getting Started
    • Azure DevOps Services
    • Reference
Powered by GitBook
On this page
  • Configure ssh
  • Ansible Inventory Configuration
  • *Try to ping the remote server
  • *Check date of all remote servers
  • Writing Ansible Playbook
  • Run the .yml file

Was this helpful?

  1. Ansible

實際操作測試1 - 使用ssh

以下是小編在看Udemy前的實作測試

Configure ssh

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub username@host

# in case you cannot do the pubkey auth, do this and it may help:
chmod g-w /home/your_user
chmod 700 /home/your_user/.ssh
chmod 600 /home/your_user/.ssh/authorized_keys

Ansible Inventory Configuration

in /etc/ansible, edit hosts :

[apache] # it is a group name of these servers
# you can specify the user and ssh port number of this server
# Inventory Parameters
# ansible_connection=ssh/winrm/local
# ansible_port=22
# ansible_user=root/administrator
# ansible_ssh_pass=password

apache2 ansible_host=syrus@192.168.1.108 ansible_port=22

*Try to ping the remote server

# you can ping the group of server by using the group name
# you can also ping all the host by using all option
# ansible all -m ping
# you can specify a different file for hosts by using -i <path/file>
$ ansible apache -m ping
apache2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

SUCCESS means we can ping the remote server.

*Check date of all remote servers

$ ansible all -i hosts -m "command" -a "date"
apache2 | CHANGED | rc=0 >>
Wed May 29 18:06:47 UTC 2019

Writing Ansible Playbook

let's take an easy one for writing the playbook!

Create a .yml file in /etc/ansible first. For example, I am going to create a apache.ymlfile:

- hosts: apache  # the group for excuting the commands.
  become: true  # use sudo
  tasks:
    - name: install apache2
      apt: name=apache2 update_cache=yes state=latest
      #update_cache stands for sudo apt-get update

Run the .yml file

ansible-playbook apache.yml --ask-become-pass
# --ask-become-pass stands for using sudo
# type host password
BECOME password: 

PLAY [apache] *******************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [apache2]

TASK [install apache2] **********************************************************************************************
 [WARNING]: Updating cache and auto-installing missing dependency: python-apt

 [WARNING]: Could not find aptitude. Using apt-get instead

ok: [apache2]

PLAY RECAP **********************************************************************************************************
apache2                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

We add task and handler for apache:

- hosts: apache
  become: true
  tasks:
    - name: install apache2
      apt: name=apache2 update_cache=yes state=latest
    - name: enabled mod_rewrite
      apache2_module: name=rewrite state=present
      notify:
        - restart apache2
  handlers:
    - name: restart apache2
      service: name=apache2 state=restarted
$ ansible-playbook apache.yml --ask-become-pass
BECOME password:

PLAY [apache] *******************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [apache2]

TASK [install apache2] **********************************************************************************************
 [WARNING]: Could not find aptitude. Using apt-get instead

ok: [apache2]

TASK [enabled mod_rewrite] ******************************************************************************************
ok: [apache2]

PLAY RECAP **********************************************************************************************************
apache2                    : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

It will update the status of remote server

PreviousSecurity - Ansible FaultNext實際操作測試2 - Docker容器部署

Last updated 5 years ago

Was this helpful?