1 安装

MAC OS 安装

1
brew install ansible

2 配置

  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
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
sudo mkdir -p /etc/ansible/
sudo cat <<EOF > /etc/ansible/hosts
all:
  #hosts:
  #  mail.example.com:
  children:
    example:
      vars:
        ansible_ssh_port: 22
        ansible_ssh_user: root
        ansible_ssh_private_key_file: ~/.ssh/id_rsa.pem
      children:
        glusterfs:
          hosts:
            glusterfs.example.com:
            glusterfs-node[01:03].example.com:
        k8s:
          hosts:
            k8s-master[01:01].example.com:
            k8s-node[01:05].example.com:
        prometheus:
          hosts:
            prometheus.example.com:
      hosts:
        k8s-master1-100-101:
          ansible_host: 192.168.100.101
        k8s-node102:
          ansible_host: 192.168.100.102
        k8s-node103:
          ansible_host: 192.168.100.103
    examplesoftware:
      vars:
        ansible_ssh_port: 22
        ansible_ssh_user: root
        ansible_ssh_private_key_file: ~/.ssh/id_rsa.pem
      children:
        kubernetes:
          hosts:
            k8s-master-01.examplesoftware.com:
            k8s-node-[01:07].examplesoftware.com:
        hadoops:
          hosts:
            namenode-[01:02].examplesoftware.com:
            datanode-[01:03].examplesoftware.com:
      hosts:
        template:
          ansible_ssh_user: es-user
          ansible_host: 192.168.1.99
    examplemobile:
      vars:
        ansible_ssh_port: 22
        ansible_ssh_user: root
        ansible_ssh_private_key_file: ~/.ssh/id_rsa.pem
      hosts:
        example-controller-1-01:
          ansible_host: 192.168.0.101
        example-controller-2-01:
          ansible_host: 192.168.0.102
        example-controller-3-01:
          ansible_host: 192.168.0.103
        example-worker-1-01:
          ansible_host: 192.168.0.104
        example-worker-2-01:
          ansible_host: 192.168.0.105
        example-worker-3-01:
          ansible_host: 192.168.0.106
        example-worker-log-01:
          ansible_host: 192.168.0.107
EOF

cat <<EOF > ~/.ansible.cfg
[defaults]
host_key_checking = False
EOF

ansible -i /etc/ansible/hosts all --list-hosts
ansible -i /etc/ansible/hosts all -m ping
ansible -i /etc/ansible/hosts all -a "/bin/echo hello"
ansible -i /etc/ansible/hosts all -a "hostname"
ansible -i /etc/ansible/hosts datanode-02.examplesoftware.com -a "hostnamectl set-hostname datanode-02"


ansible -i /etc/ansible/hosts example --list-hosts
ansible -i /etc/ansible/hosts example -m ping
ansible -i /etc/ansible/hosts example -m ansible.builtin.command -a "/bin/echo hello"
ansible -i /etc/ansible/hosts example -m ansible.builtin.command -a "hostname"
ansible -i /etc/ansible/hosts k8s-master01.example.com -m ansible.builtin.command -a "hostnamectl set-hostname k8s-master01"


ansible -i /etc/ansible/hosts k8s --list-hosts
ansible -i /etc/ansible/hosts k8s -m ping
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "/bin/echo hello"
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "hostname"
ansible -i /etc/ansible/hosts k8s-master01.example.com -m ansible.builtin.command -a "hostnamectl set-hostname k8s-master01"

ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "uname -a"
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "getenforce"
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "cat /etc/selinux/config"
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.shell -a "sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config"
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "setenforce 0"
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "getenforce"
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "date"

ansible -i /etc/ansible/hosts k8s -m ansible.builtin.shell -a 'echo "vm.swappiness = 0" >> /etc/sysctl.conf'
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "cat /etc/sysctl.conf"
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.shell -a 'sysctl -p'

ansible -i /etc/ansible/hosts examplemobile --list-hosts
ansible -i /etc/ansible/hosts examplemobile -m ping
ansible -i /etc/ansible/hosts examplemobile -m ansible.builtin.command -a "cat /etc/hosts"

3 playbook

1
2
3
cd playbooks/kubernetes/
ansible-playbook playbook-k8s-hosts.yml
ansible -i /etc/ansible/hosts k8s -m ansible.builtin.command -a "cat /etc/hosts"