Tasks 范例
下面根据常用的场景列出一些代码的范例和技巧:
通用属性
Ansible有些属性属于通用属性,适用于所有模块:
- name: Upgrade all packages to the latest version for production
  apt:
    name: "*"
    state: latest
    only_upgrade: yes
  register: result // 注册变量
  until: result.msg.find("Could not get lock /var/lib/dpkg") == -1 // 循环条件判断
  retries: // 重试次数
  delay: 10 // 重试间隔时间单位s
  failed_when: "'FAILED' in result.stdout" // 定义错误的条件,满足该条件终止
  when: common_system_upgrade and (init == '1' or init == 1) // 普通条件判断
文件管理
管理目录
- name: Create knowage directory
  file:
    path: /data/wwwroot/knowage
    state: directory
    owner: knowage
    group: knowage
# 删除目录,下载解压,重命名
# if [ ! d "scratch-gui*") ] 这种写法当不存在需要更名的目录的时候会报错,弃用
- name: Remove extra dir
  shell: rm -rf /data/wwwroot/scratch
- name: Download Scratch
  unarchive:
    src: "{{scratch_download_url}}"
    dest: /data/wwwroot
    remote_src: yes
- name: Rename dir name
  shell: if [ $( ls | grep "scratch-gui") ]; then mv scratch-gui* scratch; fi
  args:
    chdir: /data/wwwroot
创建文件
- name: Create file
  shell: if [ ! $( ls | grep "scratch-gui") ]; then touch scratch-gui; fi
  args:
    chdir: /data/wwwroot
- name: Create recurse directory
  file: 
    path: /data/xuwei/test
    state: directory
    recurse: yes
下载解压
- name: Unarchive a file that needs to be downloaded (added in 2.0)
  unarchive:
    src: https://example.com/example.zip
    dest: /usr/local/bin
    remote_src: yes
软链接
- name: Create a Apache Log symbolic link
  file:
    src: '{{item.src}}'
    dest: '{{item.dest}}'
    state: link
  with_items:
    - {src: /etc/apache2,dest: /etc/httpd}
    - {src: /usr/sbin/apache2,dest: /usr/sbin/apache}
    - {src: /usr/sbin/apache2,dest: /usr/sbin/httpd}
    - {src: /etc/apache2/sites-enabled,dest: /etc/httpd/conf.d}
    - {src: /etc/apache2,dest: /etc/httpd/conf}
    - {src: /etc/apache2/apache2.conf,dest: /etc/httpd/conf/httpd.conf}
    - {src: /etc/apache2/sites-available/000-default.conf,dest: /etc/apache2/sites-enabled/vhost.conf}
    - {src: /var/log/apache2,dest: /var/log/httpd}
    - {src: /var/log/apache2,dest: /data/logs/apache}
    - {src: /var/log/apache2,dest: /etc/apache2/logs}
    - {src: /etc/apache2/sites-enabled,dest: /data/config/apache/sites-enabled}
    - {src: /etc/apache2/mods-enabled,dest: /data/config/apache/mods-enabled}
    - {src: /etc/apache2/sites-enabled,dest: /etc/apache2/vhost}
备注: 有变量嵌套时加入单引号即可; 如下apache_version为变量:
    - {src: '/etc/apache2/apache_version/',dest: /etc/apache2/apache}
      如果目标文件夹不存在,不能创建软链接
    - {src: /etc/apache2,dest: /etc/xuwei/httpd}
替换
#匹配regexp,如果找到,用line替换;找不到,末尾追加line的内容。
- name: Change postgresql databases directory
  lineinfile:
      dest: /etc/postgresql/{{postgresql_version}}/main/postgresql.conf
      regexp: "data_directory = '/var/lib/postgresql/{{postgresql_version}}/main'"
      line: "data_directory = '/data/pgsql'"
      
 #匹配regexp,如果找到,用line替换;找不到,什么都不做。
- name: Change postgresql databases directory
  lineinfile:
      dest: /etc/postgresql/{{postgresql_version}}/main/postgresql.conf
      regexp: "data_directory = '/var/lib/postgresql/{{postgresql_version}}/main'"
      line: "data_directory = '/data/pgsql'"
      backrefs: yes
#匹配regexp,找到后删除这行;找不到,什么都不做。(这种情况下line的内容无意义)
- name: Change postgresql databases directory
  lineinfile:
      dest: /etc/postgresql/{{postgresql_version}}/main/postgresql.conf
      regexp: "data_directory = '/var/lib/postgresql/{{postgresql_version}}/main'"
      state: absent
#特殊字符的匹配,如".",需要在前面加上\进行转义,否则无法进行正则匹配(#elasticsearch.hosts 本例为替换所有#elasticsearch.hosts开头的行)
- name: elasticsearch.hosts
  lineinfile:
    path: /etc/kibana/kibana.yml
    regexp: '^#elasticsearch\.hosts'
    line: 'elasticsearch.hosts: ["http://localhost:9200"]'
    state: present
    backrefs: yes
#循环替换,数据结构
mysql_configure_extras:
  - name: innodb_buffer_pool_size
    value: 2G
  - name: innodb_log_file_size
    value: 500M
- name: Create MySQL extra databases 
  mysql_db:
    login_user: root
    login_password: '{{mysql_root_password}}'
    name: "{{ item.name }}"
    encoding: "{{ item.encoding | default('utf8mb4',true) }}"
    state: "{{ item.state | default('present',true) }}"
  with_items: "{{ mysql_databases }}"
  when: (mysql_databases is defined) and (mysql_databases != none)