玩转ansible之参数调试和文件操作篇

更多IT技术文章,欢迎关注微信公众号“运维之美”

玩转ansible之参数调试和文件操作篇

  • 01 剧本调试和帮助
  • 02 使用场景举例

上节我们学习了使用ansible进行软件安装,那么安装完软件后,就需要linux系统和软件配置修改了,对于linux主机批量修改内核参数或者软件配置,使用ansible来实现是最合适不过了。ansible语法众多,学会调试和使用命令帮助更加重要。

01 剧本调试和帮助

ansible-ploybook剧本的语法和模块很多,由于格式和其他因素可能导致报错,在使用中学会调试和检查工具,以及排错,在我看来比记语法更多重要。

  • 剧本检查
#检查Playbook的语法是否正确,但不执行任何操作
ansible-playbook --syntax-check playbook.yml

#列出Playbook中的所有任务。
ansible-playbook --list-tasks playbook.yml 

#列出Playbook中将要执行剧本的所有主机
ansible-playbook --list-hosts playbook.yml

#列出Playbook中定义的所有标签
ansible-playbook --list-tags playbook.yml

#列出所有任务的标签,包括未使用的标签
ansible-playbook --list-tags playbook.yml --tags=all

#列出所有任务以及它们的标签。
ansible-playbook --list-tags playbook.yml --list-tasks
  • 使用帮助
ansible-playbook --help:获取有关ansible-playbook命令的帮助信息。

ansible-doc -l:列出所有可用的Ansible模块及其简要描述。

ansible-doc module_name:查看特定模块的文档和使用帮助。

ansible-doc --help:获取有关ansible-doc命令的帮助信息。

例如执行ansible-doc yum除了可以看到可用参数,还可以看到使用案例

玩转ansible之参数调试和文件操作篇_第1张图片

02 使用场景举例

  • 文件创建

使用场景:创建/data目录,并在目录下创建file.txt的文件

- name: touch file
  hosts: k8s_node
  become: yes 
  tasks:
    - name: 创建一个目录
      file:
        path: /data
        state: directory
    - name: 创建一个文件
      file:
        path: /data/file.txt
        state: touch

执行结果
玩转ansible之参数调试和文件操作篇_第2张图片

  • 文件复制

等同于copy命令

 - name: Copy Files to dest dir 
  hosts: target_hosts  
  tasks:  
    - name: Copy file  
      copy:  
        src: /path/to/source/file  
        dest: /path/to/destination/file  
        owner: user  
        group: group  
        mode: 0644   
  • 删除文件

等同于rm命令

 - name: Delete Files  
  hosts: target_hosts  
  tasks:  
    - name: Delete file  
      file:  
        path: /path/to/file  
        state: absent    
  • 备份和改名
- name: 移动文件
  command: mv /opt/sourcefile.txt /tmp/newfile.txt

command模块调用linux命令和直接执行一样,只不过是批量的,效率更高

  • 追加数据

等同于linux命令sed插入数据

 - name: Insert Line in File  
  hosts: target_hosts  
  tasks:  
    - name: Insert line to file  
      lineinfile:  
        path: /path/to/file  
        line: 'hello world'    
  • 替换操作

sed也有修改替换数据的能力,使用此剧本可以修改内核参数

- name: 临时关闭 selinux
  hosts: target_hosts
  shell: "setenforce 0"
  failed_when: false
- name: 永久关闭 selinux
  hosts: target_hosts
  lineinfile:
    dest: /etc/selinux/config
    regexp: "^SELINUX="
    line: "SELINUX=disabled"  #修改为disabled后即可关闭selinux
文件查找

作用和linux命令find相同,当然你也可以通过command模块搭配find命令实现此功能

- name: find file in dir
  hosts: kube_node
  tasks:
    - name: find txt file
      find:
        paths: /opt
        file_type: file
        patterns: '*.txt'
      register: found_files

    - name: 打印结果
      debug:
        msg: "{{item.path}}"
      with_list: "{{found_files.files}}"

查找出文件后,可以进行删除或者其他后续操作

  • 判断文件是否存在
 - name: 检查目录是否存在
  hosts: kube_node
  become: yes  # 使用 sudo 或 root 权限
  tasks:
    - name: 使用 stat 模块检查目录是否存在
      stat:
        path: /tmp  # 指定要检查的目录路径
      register: dir_check

    - name: 打印检查结果
      debug:
        var: dir_check.stat.exists

这里首先使用 stat 模块来检查目标目录的存在性,将结果保存到 dir_check 变量中。然后,使用 debug 模块来打印检查结果,dir_check.stat.exists 变量将包含 True 或 False,表示目录是否存在。

  • 使用template模版

使用template模板

- name: 生成配置文件
  hosts: target_hosts
  tasks:
    - name: 使用模板生成配置文件
      template:
        src: /path/to/template.j2
        dest: /path/to/configfile.conf

通过用于将预先定义好template的文件作为模版提供给软件或者修改内核参数使用。

最近,我们建立了一个技术交流微信群。目前群里已加入了不少行业内的大神,有兴趣的同学可以加入和我们一起交流技术,在 「运维之美」 公众号直接回复 「加群」 邀请你入群。

你可能感兴趣的:(ansible)