Ansible 筆記,安裝+模塊介紹

從源碼安裝的步驟

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

82ad61ab7a624d8b9e7a880562966ecf

yum install -y epel-release wget

yum install -y python-setuptools

sudo easy_install pip

pip install paramiko PyYAML Jinja2 httplib2 six

9908b9633b42451d86d489352ac9e428

驗證下文件

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]

1c7f6856d7a24c88a55dd73ba264ae7f

測試下主機連接

ansible -m ping all

cbc82909c47c47ecb72d986c48837779

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/'

7ab9889633a440d880c73b57268a0ed2

70ecfdf38822405c9fd52df4585b0c73

file模塊

更改指定節點上/tmp/t.sh的權限爲755,屬主和屬組爲root

ansible all -m file -a "dest=/tmp/a.txt mode=755 owner=root group=root"

d46d1a3e41a44da09fcf49e6e5afd4da

因爲只有192.168.10.151有這個文件

ansible k8s -m command -a 'touch /usr/a.txt'

038e48b8ddfb4c49bc976da9c2e1dcf7

4804fa4b780b47588d7ff29b0842e590

都改了權限了

yum模塊:

在指定節點上安裝 ntp服務

ansible all -m yum -a "state=present name=ntp"

d075bedbe0db46caab2802ca81d49775

bffcff8c60b04424a7274f49f118c074

批量同步時間

ansible k8s -m command -a 'ntpdate 0.asia.pool.ntp.org'

55820f3e040a4e9b8e69e8327a28c530

cron模塊:

在指定節點上定義一個計劃任務,每隔12小時到主控端更新一次時間

ansible all -m cron -a 'name="NTP job" minute=* hour=*/12 day=* month=* weekday=* job="/usr/sbin/ntpdate 133.243.238.243"'

1a3ca9cb84e3477798d9246247fcec0f

4臺主機都有了

9391967626da4acfb4684d2b86f85274

8652376d4ade448a8ebfac8f870caeee

1e72b8fd24994d3eb96e00c1c9fd6305

7dccaef600e244ffb498894188294576

group模塊:

在所有節點上創建一個組名爲nolinux,gid爲2014的組

ansible all -m group -a 'gid=2014 name=nolinux'

2563873bee204fb3babc9cf041dc420b

user模塊:

在指定節點上創建一個用戶名爲nolinux,組爲nolinux的用戶

ansible all -m user -a 'name=nolinux groups=nolinux state=present'

d33a18bb3bb7437dbc619538350f5249

用戶刪除

ansible all -m user -a 'name=nolinux groups=nolinux state=absent remove=yes'

a2d6a3dccc79473f8e06b349b7746b02

service模塊:

啓動指定節點上的 puppet 服務,並讓其開機自啓動

ansible all -m service -a 'name=ntpdate state=restarted enabled=yes'

f9fd787118da40128079e580dea59826

script模塊:

在指定節點上執行/root/a.sh腳本(該腳本是在ansible控制節點上的)

ansible all -m script -a '/root/a.sh'

command模塊:

在指定節點上運行hostname命令

ansible all -m command -a 'hostname'

7d9b347e25674d84858ef5f733cb7833

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'

3a91b4cbc251455e89b8463a3950b071

同步2個目錄在多個主機中

touch /tmp/3.txt

ansible all -m synchronize -a 'src=/tmp/ dest=/tmp/ compress=yes'

可以看到其他主機中3.txt已經同步過去了

09ee7194993b4b038752fecc69123331

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章