Ansible 自動化常用實例

安裝ansible
[root@ansible ~]# yum install -y ansible

修改管理哪些主機的清單文件
[root@ansible ansible]# vi /etc/ansible/hosts
[webserver]
192.168.1.21
192.168.1.22
192.168.1.23

給ansible和被管理的主機之間做一個密鑰登錄
[root@ansible ansible]# ssh-keygen
[root@ansible ansible]# ssh-copy-id 192.168.1.21

管理主機分兩種方式(命令/劇本)
命令:
命令格式
ansible <hosts> [options]

例子1: ping 測通是否通暢
[root@ansible ansible]# ansible webserver -m ping -u root
...省略

例子2: 調用目錄下的echo命令
[root@ansible ansible]# ansible all -a "/bin/echo hello world"
...省略

例子3: copy文件到另一個目錄
[root@ansible ansible]# ansible webserver -m copy -a "src=/etc/passwd dest=/opt/passwd"

...省略

例子4: 安裝軟件
[root@ansible ansible]# ansible webserver -m yum -a "name=lrzsz"
...省略

例子5: 添加用戶
[root@ansible ansible]# ansible webserver -m user -a "name=zhangsan password=123"

例子6: 啓動系統的某個服務
[root@ansible ansible]# ansible webserver -m service -a "name=sshd state=started"
...省略
[root@ansible ansible]# ansible webserver -m service -a "name=httpd state=started"
...省略
[root@ansible ansible]# ansible webserver -m service -a 'name=httpd state=restarted'
...省略

例子7: 重啓某個服務
[root@ansible ansible]# ansible webserver -m service -a "name=httpd state=restarted"
...省略

例子8: 指定3臺機器執行同一個命令(屬於並行執行)
[root@ansible ansible]# ansible webserver -a "echo hello" -f 3
...省略

例子9: 獲取系統信息
[root@ansible ansible]# ansible webserver -m setup
...省略

劇本:
Playbook組成

hosts: 目標主機
remote_user: 執行操作的用戶身份
vars: 執行中的一些變量
tasks: 定義順序執行的action,每個action調用一個模塊
handers: event處理操作,僅有在action觸發時纔會執行,多次觸發只執行一次並按照聲明的順序執行。

例子10: 安裝httpd服務
[root@ansible /]# vi test.yml

  • hosts: webserver
    remote_user: root
    tasks:

    • name: install httpd
      yum: pkg=httpd state=latest
  • hosts: webserver
    remote_user: root
    tasks:
    • name: start httpd
      service: name=httpd state=started

例子11: 獲取debug信息
[root@ansible /]# vi debug.yml

  • hosts: webserver
    remote_user: root
    tasks:
    • name: debug
      debug:
      msg: "{{ansible_default_ipv4.gateway}}"

例子12: shell模塊
[root@ansible /]# vi shell.yml

  • hosts: webserver
    remote_user: root
    tasks:
    • name: guanbifanghuoqiang
      shell: systemctl stop firewalld

例子13: 拷貝模塊
[root@ansible /]# vi copy.yml

  • hosts: all
    remote_user: root
    tasks:

    • name: copy
      copy: src=/etc/passwd dest=/home
      例子14:創建用戶 然後再 刪除用戶 執行雙任務
      [root@ansible /]# vi user.yml
  • hosts: all
    remote_user: root
    tasks:

    • name: create user
      user:
      name: apeng
      uid: 5000
      group: ftp
      shell: /bin/bash
      groups: apeng
      append: yes

    • name: delete user
      user:
      name: apeng
      state: absent
      remove: yes

例子15: 安裝httpd 然後再 卸載
[root@ansible /]# vi yum.yml

  • hosts: all
    remote_user: root
    tasks:
    • name: install httpd
      yum:
      name: httpd
      state: latest
    • name: remove httpd
      yum:
      name: httpd
      state: absent

實例16: command模塊
[root@ansible /]# vi command.yml

  • hosts: root
    remote_user: root
    tasks:
    • name: cmd
      command: ls
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章