整理自网络 方便查看
对变量使用正则
- name: Extract tomcat archive
command: chdir=/opt/software /bin/tar xvf /tmp/{{tomcat_bin_file}} -C /opt/software creates={{tomcat_bin_file|regex_replace('(.*).tar.gz$', '\\1') }
取目录中文件为变量
- name: check conf.d
shell: (cd /etc/nginx/conf.d/; find . -maxdepth 1 -type f) | cut -d'/' -f2
register: files_to_fetch
tags: fetch_nginx_conf
- name: fetch conf.d
fetch: src=/etc/nginx/conf.d/{{ item }}
dest=conf_files/nginx_conf/{{ansible_hostname}}_{{ansible_default_ipv4.address}}/conf.d/
flat=yes
with_items: files_to_fetch.stdout_lines
指定组执行
- name: my command
command: echo stuff
when: "'groupname' not in group_names"
指定不执行
ansible-playbook --limit 'all:!oadb01' sync_repos.yml
ip address
{{ ansible_default_ipv4.address }}
{{ ansible_eth0.ipv4.address }}
判断目录是否挂载
- name: check if /opt/xxx is mounted or not
command: mountpoint -q /opt/xxx
register: volume_stat
failed_when: False
changed_when: False
- name: debug
debug: msg="/opt/xxx is mounted"
when: volume_stat.rc == 0