ansible劇本編寫擴展功能

1、劇本編寫循環(huán)功能

實現(xiàn)一個模塊干多件事情;
方法一:

- hosts: 172.16.1.7
  tasks:
    - name: creat user01-10
      user: name={{ item }} shell=/sbin/nologin create_home=no
      with_items:
        - user01
        - user02
        - user03

方法二:

    - hosts: 172.16.1.7
      tasks:
        - name: create user01-03
          user: name={{ item.name }}  shell={{ item.shell }} create_home={{ item.home }}
          with_items:
            - {name: 'user01', shell: '/sbin/nologin', home: 'yes'}
            - {name: 'user02', shell: '/bin/bash', home: 'no'}
            - {name: 'user03', shell: '/bin/bash', home: 'yes'}

2、劇本編寫判斷功能

    - hosts: rsync
      tasks:
        - name: create password file
          copy: content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600
          when: (ansible_hostname == "backup")
        - name: create password file for rsync_client
          copy: content='oldboy123' dest=/etc/rsync.password mode=600
          when: (ansible_hostname != "backup")

3、ansible常見主機信息

ansible_all_ipv4_addresses: 僅顯示ipv4的信息。
ansible_devices: 僅顯示磁盤設(shè)備信息。
ansible_distribution: 顯示是什么系統(tǒng),例:centos,suse等。
ansible_distribution_major_version: 顯示是系統(tǒng)主版本。
ansible_distribution_version: 僅顯示系統(tǒng)版本。
ansible_machine: 顯示系統(tǒng)類型,例:32位,還是64位。
ansible_eth0: 僅顯示eth0的信息。
ansible_hostname: 僅顯示主機名。
ansible_kernel: 僅顯示內(nèi)核版本。

ansible_lvm: 顯示lvm相關(guān)信息。
ansible_memtotal_mb: 顯示系統(tǒng)總內(nèi)存。
ansible_memfree_mb: 顯示可用系統(tǒng)內(nèi)存。
ansible_memory_mb: 詳細(xì)顯示內(nèi)存情況。
ansible_swaptotal_mb: 顯示總的swap內(nèi)存。
ansible_swapfree_mb: 顯示swap內(nèi)存的可用內(nèi)存。
ansible_mounts: 顯示系統(tǒng)磁盤掛載情況。
ansible_processor: 顯示cpu個數(shù)(具體顯示每個cpu的型號)。
ansible_processor_vcpus: 顯示cpu個數(shù)(只顯示總的個數(shù))。

4、忽略錯誤

因前面腳本出現(xiàn)錯誤,后續(xù)腳本也不會執(zhí)行,所以要學(xué)會忽略錯誤,便于調(diào)試。

- hosts: 172.16.1.7
  tasks:
   - name: create user
     shell: useradd oldboy100
     ignore_errors: yes
   - name: copy file
     copy: src=/etc/hosts  dest=/tmp
   - name: file attr
     file: path=/etc/hosts mode=600

5、標(biāo)簽功能

針對某個點進(jìn)行執(zhí)行操作,單獨執(zhí)行錯誤的操作。
ansible-playbook -t test01 test_標(biāo)簽功能.yaml

- hosts: 172.16.1.7
  tasks:
    - name: create user
      shell: useradd oldboy100
      ignore_errors: yes
      tags: test01
    - name: copy file
      copy: src=/etc/hosts  dest=/tmp
    - name: file attr
      file: path=/etc/hosts mode=600

6、忽略采集功能

用于管理多臺主機的提升執(zhí)行效率,忽略收集過程。

- hosts: 172.16.1.7
  gather_facts: no      --- 省略主機信息收集過程/確保是否還需要使用判斷功能
  tasks:
    - name: create user
      shell: useradd oldboy100
      ignore_errors: yes
      tags: test01
    - name: copy file
      copy: src=/etc/hosts  dest=/tmp
    - name: file attr
      file: path=/etc/hosts mode=600

7、劇本觸發(fā)功能

- hosts: 172.16.1.41
      tasks:
        - name: copy file
          copy: src=rsyncd.conf  dest=/etc/
          notify: rsync_server
        - name: boot server
          service: name=rsyncd state=started
    
      handlers:
        - name: rsync_server
          service: name=rsyncd state=restarted

8、將劇本進(jìn)行整合

方式一:include_tasks: f1.yml

  • hosts: all
    remote_user: root
    tasks:
    • include_tasks: f1.yml
    • include_tasks: f2.yml

實踐操作:

  • hosts: 172.16.1.41
    tasks:
    • include_tasks: rsync_auto.yaml
  • hosts: 172.16.1.31
    tasks:
    • include_tasks: nfs_auto.yaml

方式二:include: f1.yml

  • include:f1.yml
  • include:f2.yml

方式三:- import_playbook:
[root@m01 ansible-playbook]# cat main.yml

  • import_playbook: base.yml
  • import_playbook: rsync.yml
  • import_playbook: nfs.yml
  • import_playbook: oxxx.yml
  • import_playbook: rsync.yml
  • import_playbook: nfs.yml
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容