1、基於密碼的方式
[root@localhost ~]# vim /etc/ansible/hosts
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[web02]
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[root@localhost ~]# ssh 192.168.13.100
[root@localhost ~]# ansible web02 -m ping
ping所有
[root@localhost ~]# ansible all -m ping
ansible_ssh_user :使用者名稱
ansible_ssh_port :埠
ansible_ssh_pass :密碼
2、基於變數密碼的方式
[root@localhost ~]# vim /etc/ansible/hosts
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22
[web01:vars]
ansible_ssh_pass='1'
3、乙個分組配置多主機
[root@localhost ~]# cat /etc/ansible/hosts
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22
[web01:vars]
ansible_ssh_pass='1'
4、基於金鑰的方式登入
生成金鑰:
[root@localhost ~]# ssh-keygen
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@localhost ~]# ssh [email protected]
[root@localhost ~]# cat /etc/ansible/hosts
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22
5、分組組合
[root@localhost ~]# cat /etc/ansible/hosts
[web01]
192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22
[web02]
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[web:children]
web01
web02
[root@localhost ~]# ansible web -m ping
1、ansible-hoc 臨時命令
2、ansible-hoc 返回結果
綠色: 代表被管理端主機沒有被修改
黃色: 代表被管理端主機發現變更
紅色: 代表出現了故障,注意檢視提示
2.1、command模組command模組是ansible中預設模組。
引數: chdir : 執行命令的目錄
creates:在執行命令之前,判斷是否已經存在該路徑
案例: 使用 command 模組建立100個資料夾
[root@localhost test]# ansible web02 -m command -a 'mkdir /root/test/'
2.2、shell模組shell 模組跟 command 模組功能非常相似,都是執行命令的模組;但是shell模組支援特殊符號,效能沒有 command 模組高。
引數: chdir : 執行命令的目錄
removes :判斷乙個檔案是否存在,存在則執行。
[root@localhost 1]# ansible all -m shell -a 'mkdir abc; removes=/root/test/1'
creates:在執行命令之前,判斷是否已經存在該路徑
案例: 將 /etc 目錄中檔名稱包含 root 的檔案打包到 /tmp 目錄
[root@localhost ~]# ansible all -m shell -a "tar -czvpf /tmp/root.tar.gz `find /etc/ -name '*root*' | xargs`"
3.3、script模組script模組主要是用來執行指令碼檔案的。
引數: chdir : 執行命令的目錄
removes :判斷乙個檔案是否存在,存在則執行。
creates :在執行命令之前,判斷是否已經存在該路徑
[root@localhost ~]# ansible all -m script -a './in.sh'
3.4、yum模組安裝軟體包的模組。
引數: name : 軟體包名稱
state :指定 yum 模組執行的狀態
利用 yum 模組,安裝部署mariadb
[root@localhost ~]# ansible all -m yum -a 'name=mariadb* state=present'
3.5、yum_repository模組yum倉庫模組
引數: description : 倉庫描述
enabled : 是否啟用
gpgcheck :是否驗證gpg key
name : 倉庫名稱
[root@localhost yum.repos.d]# ansible all -m yum_repository -a 'name=nginx-stable description="nginx stable repo" baseurl= gpgcheck=yes gpgkey= enabled=yes'
案例: 解除安裝所有版本的nginx,使用官方倉庫安裝nginx
[root@localhost yum.repos.d]# ansible all -m yum -a 'name=nginx state=absent'
[root@localhost yum.repos.d]# ansible all -m yum_repository -a 'name=nginx-stable description="nginx stable repo" baseurl= gpgcheck=yes gpgkey= enabled=yes'
[root@localhost yum.repos.d]# ansible all -m yum_repository -a 'name=epel baseurl= description=epel enabled=no'
[root@localhost yum.repos.d]#\\192.168.13.28\linux14ansible all -m yum -a 'name=nginx state=latest'
Ansible2 主機清單
ansible 通過讀取預設的主機清單配置 etc ansible hosts,可以同時連線到多個遠端主機上執行任務,預設路徑可以通過修改 ansible.cfg 的 hostfile 引數指定路徑。對於 etc ansible hosts最簡單的定義格式像下面 1 簡單的主機和組 mail yan...
Ansible之二 主機清單
ansible 通過讀取預設的主機清單配置 etc ansible hosts,可以同時連線到多個遠端主機上執行任務,預設路徑可以通過修改 ansible.cfg 的 hostfile 引數指定路徑。對於 etc ansible hosts最簡單的定義格式像下面 1 簡單的主機和組 mail.yan...
Ansible 主機配置清單檔案
參考至官方文件,官方文件包含了清單檔案的 yaml 寫法 在通過 ansible 操作目標主機之前,你需要先在 inventory 主機清單 中配置目標主機資訊。預設情況下主機清單儲存在系統的 etc ansible hosts 檔案中,你也可以通過命令列選項指定其它的清單檔案 i 主機清單配置預設...