本文不定期更新 :)

A system administrator’s guide to getting started with Ansible

ad-hoc

管理集群的时候,常常来不及写playbooks,只需要执行一些ad-hoc查看某些主机的状态, 或者批量修改/上传配置文件到某些主机。

1
2
ansible all -m copy -a \
'src=dvd.repo dest=/etc/yum.repos.d owner=root group=root mode=0644' -b

playbook

1
ansible-playbook -i prod_hosts demo.yml --skip-tag downloaded

host file

1
2
3
4
5
6
7
[api]
tt ansible_host=test
tt3 ansible_host=test3
tt8 ansible_host=test8
[db]
pg1 ansible_host=db88
pg2 ansible_host=db98

task

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# demo.yml
---
- hosts: db
  vars:
    tar_src: "tars/postgres_exporter_v0.4.1_linux-amd64.tar.gz"
    tar_dest: "/usr/bin/"
    service_src: "services/postgres_exporter.service"
    service_dest: "/usr/lib/systemd/system/" # works on centos; ubuntu is '/etc/systemd/system/

  tasks:
  - debug: var=ansible_default_ipv4.address

  - name:  untar to /usr/bin
    unarchive:
      src: "{{ tar_src }}"
      dest: "{{ tar_dest }}"
    become: true

  - name: download and untar prometheus tarball
    tags: downloaded
      unarchive:
        src: "{{ prometheus_tarball_url }}"
        dest: "{{ prometheus_install_path }}"
        copy: no
   
  - name: copy service file
    copy:
      src: "{{ service_src }}"
      dest: "{{ service_dest }}"
    become: true

  - name: ensure node_export is ebalbe and running
    systemd:
      name: postgres_exporter
      enabled: yes
      daemon_reload: yes
      state: started
    become: true