Ansible常用模塊

sysctl模塊

管理sysctl.conf文件,可用來管理內核參數

常用參數 默認值 註釋
name 指定sysctl支持的變量,內核參數可以在這裏管理
state present 定義該值是否應該在sysctl文件中存在,present存在,absent不存在
sysctl_file “/etc/sysctl.conf” 設置sysctl.conf文件位置
value name變量對應的值
- name: Setting present kernel params
  sysctl:
    name: "{{ item.name }}"
    value: "{{ item.value }}"
    ignoreerrors: yes
    state: present
  with_items:
    - { name: 'net.core.rmem_default', value: 8388608 }
    - { name: 'net.core.rmem_max', value: 16777216  }
    - { name: 'net.core.wmem_default', value: 8388608  }
    - { name: 'net.core.wmem_max', value: 16777216 }
    - { name: 'net.ipv4.igmp_max_memberships', value: 512  }
    - { name: 'net.ipv4.conf.bond0.force_igmp_version', value: 2  }
    - { name: 'vm.max_map_count', value: 655360 }
  become: yes

systemd模塊

管理services

常用參數 默認值 註釋
daemon_reload no 任何操作之前先執行daemon-reload操作,no/yes
enabled 設置是否開機啓動,no/yes
name 服務名稱
state reloaded重載,restarted重啓,started啓動,stopped停止
- name: Stop and Disable service
  systemd:
    name: "{{ item }}"
    state: stopped
    enabled: no
  with_items:
    - firewalld
    - irqbalance
  become: yes

selinux模塊

管理selinx的狀態和策略

  • 配置SELinux模式和策略
  • 使用後可能需要重啓,比如本來是開啓的現在狀態定義爲disabled就需要重啓生效,required可以設置爲yes會幫你重啓,默認no
  • 不會自動幫你重啓,但是重啓生效的會有提示信息
常用參數 默認值 註釋
configfile “/etc/selinux/config” 文件路徑,默認"/etc/selinux/config"
policy SELinux的策略
state The SELinux mode.disabled,enforcing,permissive
- name: Stop SELinux
  selinux:
    policy: targeted
    state: permissive
  become: yes

- name: Disable SELinux
  selinux:
    state: disabled
  become: yes

blockinfile 模塊

插入/更新/移除被標記線環繞的文本信息

常用參數 默認值 註釋
backup no 如果爲yes表示創建一個包含時間戳信息的備份文件,如果錯誤修改了可以獲得源文件
block “” 要插入標記行內的文本,如果爲空串這個塊會被刪除
create no 如果此文件不存在,創建一個新文件
group 文件屬組
insertafter EOF 如果使用這個參數,將會再最後一次匹配到正則表達式之後插入塊,EOF意味着再文件最後插入這一行,如果指定的正則表達式沒有匹配成功,EOF將會生效
insertbefore EOF insertafter類似不同點就是會在最後一次匹配正則表達式成功之前插入塊
marker “# {mark} ANSIBLE MANAGED BLOCK” 標記信息,標記信息的模板
marker_begin “BEGIN” 會替換掉標記信息的{mark}作爲標識塊的開始
marker_end “END” 會替換掉標記信息的{mark}作爲標識塊的開始
mode 設置文件權限
owner 文件屬主
path 修改的文件路徑
state present 該塊是否應該存在,默認present,不應該存在就填寫absent
- name: Update /etc/security/limits.conf
  blockinfile:
    dest: /etc/security/limits.conf
    insertbefore: '# End of file'
    block: |
      * soft nofile 131072
      * hard nofile 131072
      * soft core unlimited
      * hard core unlimited
      * soft memlock unlimited
      * hard memlock unlimited
  become: yes

執行之後的效果爲

# BEGIN ANSIBLE MANAGED BLOCK
* soft nofile 131072
* hard nofile 131072
* soft core unlimited
* hard core unlimited
* soft memlock unlimited
* hard memlock unlimited
# END ANSIBLE MANAGED BLOCK

在文件的結尾會有該塊信息,注意如果手動刪除了標識符,會導致無法正確定位到塊

參考文檔

  • https://docs.ansible.com/ansible/latest/modules/systemd_module.html
  • https://docs.ansible.com/ansible/latest/modules/sysctl_module.html
  • https://docs.ansible.com/ansible/latest/modules/selinux_module.html
  • https://docs.ansible.com/ansible/latest/modules/blockinfile_module.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章