從源碼安裝的步驟
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible
source ./hacking/env-setup
yum install -y epel-release wget
yum install -y python-setuptools
sudo easy_install pip
pip install paramiko PyYAML Jinja2 httplib2 six
驗證下文件
git pull --rebase
git submodule update --init --recursive
從yum安裝
yum install -y epel
yum install -y ansible
我是先看到二進制源碼安裝的,沒想到yum這麼粗暴,如果您跟着我的筆記做過源代碼安裝,那麼恭喜你,你玩過2種安裝方式鳥
host中添加本機,然後測試下
echo "127.0.0.1" > ~/ansible_hosts
export ANSIBLE_HOSTS=~/ansible_hosts
建立主機列表
cat << EOF > /etc/ansible/hosts
[k8s]
192.168.10.151
192.168.10.152
192.168.10.153
192.168.10.154
EOF
免密碼登錄
ssh-keygen
ssh-copy-id [email protected]
測試下主機連接
ansible -m ping all
ansible常用的13個模塊
copy模塊
file模塊
cron模塊
group模塊
user模塊
yum模塊
service模塊
script模塊
ping模塊
command模塊
raw模塊
get_url模塊
synchronize模塊
copy模塊:
把主控端/root目錄下的a.sh文件拷貝到到指定節點上
ansible 192.168.10.151 -m copy -a 'src=/usr/a.txt dest=/tmp/'
file模塊
更改指定節點上/tmp/t.sh的權限爲755,屬主和屬組爲root
ansible all -m file -a "dest=/tmp/a.txt mode=755 owner=root group=root"
因爲只有192.168.10.151有這個文件
ansible k8s -m command -a 'touch /usr/a.txt'
都改了權限了
yum模塊:
在指定節點上安裝 ntp服務
ansible all -m yum -a "state=present name=ntp"
批量同步時間
ansible k8s -m command -a 'ntpdate 0.asia.pool.ntp.org'
cron模塊:
在指定節點上定義一個計劃任務,每隔12小時到主控端更新一次時間
ansible all -m cron -a 'name="NTP job" minute=* hour=*/12 day=* month=* weekday=* job="/usr/sbin/ntpdate 133.243.238.243"'
4臺主機都有了
group模塊:
在所有節點上創建一個組名爲nolinux,gid爲2014的組
ansible all -m group -a 'gid=2014 name=nolinux'
user模塊:
在指定節點上創建一個用戶名爲nolinux,組爲nolinux的用戶
ansible all -m user -a 'name=nolinux groups=nolinux state=present'
用戶刪除
ansible all -m user -a 'name=nolinux groups=nolinux state=absent remove=yes'
service模塊:
啓動指定節點上的 puppet 服務,並讓其開機自啓動
ansible all -m service -a 'name=ntpdate state=restarted enabled=yes'
script模塊:
在指定節點上執行/root/a.sh腳本(該腳本是在ansible控制節點上的)
ansible all -m script -a '/root/a.sh'
command模塊:
在指定節點上運行hostname命令
ansible all -m command -a 'hostname'
get_url模塊:
將http://1.1.1.1/favicon.ico文件下載到指定節點的/tmp目錄下
ansible 1.1.1.1 -m get_url -a 'url=http://1.1.1.1/favicon.ico dest=/tmp'
sync模塊
https://docs.ansible.com/ansible/latest/modules/synchronize_module.html
先確定有沒有安裝rsync
ansible all -m yum -a "state=present name=rsync"
ansible all -m synchronize -a 'src=/usr/b.txt dest=/tmp/ compress=yes'
ansible all -m command -a 'ls /tmp/b.txt'
同步2個目錄在多個主機中
touch /tmp/3.txt
ansible all -m synchronize -a 'src=/tmp/ dest=/tmp/ compress=yes'
可以看到其他主機中3.txt已經同步過去了
pip install --upgrade pyls https://pypi.python.org/simple
sudo easy_install pip
ansible all -m ping --ask-pass
設置dns
cat << EOF >> /etc/hosts
192.168.10.151 K8S-M1
192.168.10.152 K8S-M2
192.168.10.153 K8S-M3
192.168.10.154 K8S-M4
EOF