Install by docker

Docker images

docker pull ansible/container-conductor-alpine-3.5

Gen know_host

ssh user@host

ssh key

ssh-keygen -t rsa -b 4096
ssh-copy-id user@host

if passworded

eval $(ssh-agent) (if not running)
ssh-add ~/.ssh/id_rsa

ansible command

ansible [pattern] -m [module] -a "[module options]"

  • [pattern] : all ,host,group
  • [module] : module name ,default =command
  • [module options] : args for module

Example

ansible all -a "/bin/echo hello"
  • -b : sudo
  • -K : ask password

inventory

host1 ansible_host=192.0.2.50 ansible_port=222 ansible_user=name
host2 ansible_host=192.0.2.51 ansible_port=22 ansible_user=name
host3 ansible_host=192.0.2.52 ansible_port=22 ansible_user=name
[test01]
host1

[test02]
host2
host3

ansible.cfg

[defaults]
inventory=dir/file_name

DIY newer version

Docker Images

docker pull alpine:3.11.2

Dockerfile

FROM alpine:3.11.2
LABEL version="1.0.0"
LABEL description="ansible"
RUN ["apk","add","ansible"]
RUN ["apk","add","openssh-client"]
RUN ["apk","add","git"]
WORKDIR  /root/projects
VOLUME ["/root/.ssh","/root/projects"]

template

  • {{ item }} 迴圈index
  • {{ ansible_facts.hostname }} hostname
  • {{ 自定義 }}

playbook depoloy sample

- hosts: some_host_name
  become: yes
  vars:
   docker_container: 'docker_container_name'
   docker_image: 'docker_image_name'
   docker_image_ver: '0.0.1'
   docker_dir: '/docker_data/app_name'
   export_port: 8000
   loop_count: ['']
#"{{ range(0, 3 + 1, 1)|list }}"
  tasks:
   - name: Get docker container ID
     command:
      cmd: "docker ps  -f name=^/{{ docker_container }}{{ item }}$ -q"
     register: dcoker_query_result
     loop: "{{ loop_count }}"
   - name: Stop docker container
     command:
      cmd: docker stop {{ item.stdout }}
     loop: "{{ dcoker_query_result.results }}"
     when: item.stdout != ''
   - name: Rm docker container
     command:
      cmd: docker rm {{ item.stdout }}
     loop: "{{ dcoker_query_result.results }}"
     when: item.stdout != ''
   - name: Create config directory          
     file:
      path: "{{ docker_dir }}{{ item }}/config"
      state: directory
     loop: "{{ loop_count }}"
   - name: Create Logs directory    
     file:
      path: "{{ docker_dir }}{{ item }}/Logs"
      state: directory
     loop: "{{ loop_count }}"
   - name: Generated application.properties
     template:
      src: template/application.properties
      dest: "{{ docker_dir }}{{ item }}/config/application.properties"
     loop: "{{ loop_count }}"
   - name: Copy log4j2-spring.xml
     copy:
      src: template/log4j2-spring.xml
      dest: "{{ docker_dir }}{{ item }}/config/log4j2-spring.xml"
     loop: "{{ loop_count }}"      
   - name: Deploy docker
     command:
      argv: 
       - docker
       - run
       - '-d'
       - '--name'
       - "{{ docker_container }}{{ item }}"
       - '-p'
       - "{{ export_port + ansible_loop.index0 }}:8080"
       - "-m"
       - "3g"
       - "--cpus=3"
       - '-v'
       - "{{ docker_dir }}{{ item }}/config:/APP/config"
       - '-v'
       - "{{ docker_dir }}{{ item }}/Logs:/APP/logs"
       - {{ docker_image }}:{{ docker_image_ver }}
     loop: "{{ loop_count }}"
     loop_control:
      extended: yes

playbook prometheus example

- hosts: monitor
  become: yes
  vars:
   meteric_name: my_app_name
   monitors_host: ['172.16.41.171:8000','172.16.41.172:8000','172.16.41.173:8000',
                   '172.16.41.175:8000','172.16.41.176:8000','172.16.41.177:8000',
                   '172.16.41.178:8000','172.16.41.179:8000','172.16.41.180:8000',
                   '172.16.41.181:8000','172.16.41.182:8000','172.16.41.183:8000',
                   '172.16.41.184:8000']
  tasks:
   - name: prometheus meteric
     blockinfile:
      path: /data/prometheus/config/prometheus.yml
      insertafter: EOF
      marker: "#{mark} {{ meteric_name }}"
      block: "  - job_name: '{{meteric_name}}'\n    metrics_path: '/actuator/prometheus'\n    static_configs:\n       - targets: {{ monitors_host|string }}"
   - name: Stop prometheus docker
     command:
      cmd: docker stop prometheus
   - pause:
      seconds: 2
   - name: Start prometheus docker
     command:
      cmd: docker start prometheus