ansible-playbook常用模块

lineinfile

此模块是针对文件特殊行,使用后端引用的正则表达式来替换。

- hosts: 192.168.50.1
  gather_facts: no
  tasks:
    - name: 设置UseDNS为no
       lineinfile:
          dest: /etc/ssh/sshd_config   # 需要修改的配置文件
          backrefs: yes                # 1.regexp匹配的话则替换成line 2.不匹配的话则添加line
          regexp: '^#UseDNS yes'       # 寻找以#UseDNS yes开头的行
          line: 'UseDNS no'            # 将regexp匹配到的行替换成line
    - name: 重启SSHD服务
        service: 
          name: sshd
          state: restarted
    - name: add nouveau to blacklist   # 增加一行配置
       lineinfile:
          path: /etc/modprobe.d/blacklist.conf
          line: blacklist nouveau
          create: true

copy content

此模块适用于我们不想在部署服务器提前预置文件,直接部署安装的时候,直接写入文件:

- name: create repo
   copy:
      dest: /etc/yum.repos.d/base.repo
      content: |
        [test-base]
        name=test base repository
        baseurl=file:///tmp/test/rpm/
        enabled=0
        gpgcheck=0
- name: "Ssh | start sshd.service"
  systemd:
    daemon_reload: true
    enabled: yes
    name: sshd
    state: started
  tags: ssh
posted @ 2020-06-11 11:38  yuhaohao  阅读(301)  评论(0编辑  收藏  举报