Ansible文件模块

文件模块

序号 模块名 英文解释 中文说明
1 acl Sets and retrieves file ACL information -
2 archive Creates a compressed archive of one or more files or trees 压缩文件
3 assemble Assembles a configuration file from fragments -
4 blockinfile Insert/update/remove a text block surrounded by marker lines -
5 copy Copies files to remote locations 拷贝文件
6 fetch Fetches a file from remote nodes 从远程节点拉取一个文件
7 file Sets attributes of files 给文件设置属性
8 find Return a list of files based on specific criteria -
9 ini_file Tweak settings in INI files -
10 iso_extract Extract files from an ISO image -
11 lineinfile Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression
12 patch Apply patch files using the GNU patch tool -
13 replace Replace all instances of a particular string in a file using a back-referenced regular expression -
14 stat Retrieve file or file system status -
15 synchronize A wrapper around rsync to make common tasks in your playbooks quick and easy -
16 tempfile Creates temporary files and directories -
17 template Templates a file out to a remote server -
18 unarchive Unpacks an archive after (optionally) copying it from the local machine 解压一个从本地拷贝的文件
19 xattr set/retrieve extended attributes -
20 xml Manage bits and pieces of XML files or strings -

示例

acl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Grant user Joe read access to a file
- acl:
path: /etc/foo.conf
entity: joe
etype: user
permissions: r
state: present

# Removes the acl for Joe on a specific file
- acl:
path: /etc/foo.conf
entity: joe
etype: user
state: absent

# Sets default acl for joe on foo.d
- acl:
path: /etc/foo.d
entity: joe
etype: user
permissions: rw
default: yes
state: present

# Same as previous but using entry shorthand
- acl:
path: /etc/foo.d
entry: "default:user:joe:rw-"
state: present

# Obtain the acl for a specific file
- acl:
path: /etc/foo.conf
register: acl_info

archive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Compress directory /path/to/foo/ into /path/to/foo.tgz
- archive:
path: /path/to/foo
dest: /path/to/foo.tgz

# Compress regular file /path/to/foo into /path/to/foo.gz and remove it
- archive:
path: /path/to/foo
remove: True

# Create a zip archive of /path/to/foo
- archive:
path: /path/to/foo
format: zip

# Create a bz2 archive of multiple files, rooted at /path
- archive:
path:
- /path/to/foo
- /path/wong/foo
dest: /path/file.tar.bz2
format: bz2

# Create a bz2 archive of a globbed path, while excluding specific dirnames - archive:
path:
- /path/to/foo/*
dest: /path/file.tar.bz2
exclude_path:
- /path/to/foo/bar
- /path/to/foo/baz
format: bz2

# Create a bz2 archive of a globbed path, while excluding a glob of dirnames
path:
- /path/to/foo/*
dest: /path/file.tar.bz2
exclude_path:
- /path/to/foo/ba*
format: bz2

assemble

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Example from Ansible Playbooks
- assemble:
src: /etc/someapp/fragments
dest: /etc/someapp/someapp.conf

# When a delimiter is specified, it will be inserted in between each fragment
- assemble:
src: /etc/someapp/fragments
dest: /etc/someapp/someapp.conf
delimiter: '### START FRAGMENT ###'

# Copy a new "sshd_config" file into place, after passing validation with sshd
- assemble:
src: /etc/ssh/conf.d/
dest: /etc/ssh/sshd_config
validate: '/usr/sbin/sshd -t -f %s'

blockinfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Before 2.3, option 'dest' or 'name' was used instead of 'path'
- name: insert/update "Match User" configuration block in /etc/ssh/sshd_config
blockinfile:
path: /etc/ssh/sshd_config
block: |
Match User ansible-agent
PasswordAuthentication no

- name: insert/update eth0 configuration stanza in /etc/network/interfaces
(it might be better to copy files into /etc/network/interfaces.d/)
blockinfile:
path: /etc/network/interfaces
block: |
iface eth0 inet static
address 192.0.2.23
netmask 255.255.255.0

- name: insert/update configuration using a local file
blockinfile:
block: "{\{ lookup('file', './local/ssh_config') }\}"
dest: "/etc/ssh/ssh_config"
backup: yes

- name: insert/update HTML surrounded by custom markers after <body> line
blockinfile:
path: /var/www/html/index.html
marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
insertafter: "<body>"
content: |
<h1>Welcome to {\{ ansible_hostname }\}</h1>
<p>Last updated on {\{ ansible_date_time.iso8601 }\}</p>

- name: remove HTML as well as surrounding markers
blockinfile:
path: /var/www/html/index.html
marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
content: ""

- name: Add mappings to /etc/hosts
blockinfile:
path: /etc/hosts
block: |
{\{ item.ip }\} {\{ item.name }\}
marker: "# {mark} ANSIBLE MANAGED BLOCK {\{ item.name }\}"
with_items:
- { name: host1, ip: 10.10.1.10 }
- { name: host2, ip: 10.10.1.11 }
- { name: host3, ip: 10.10.1.12 }

copy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Example from Ansible Playbooks
- copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: 0644

# The same example as above, but using a symbolic mode equivalent to 0644
- copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: u=rw,g=r,o=r

# Another symbolic mode example, adding some permissions and removing others
- copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: u+rw,g-wx,o-rwx

# Copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
- copy:
src: /mine/ntp.conf
dest: /etc/ntp.conf
owner: root
group: root
mode: 0644
backup: yes

# Copy a new "sudoers" file into place, after passing validation with visudo
- copy:
src: /mine/sudoers
dest: /etc/sudoers
validate: /usr/sbin/visudo -cf %s

# Copy a "sudoers" file on the remote machine for editing
- copy:
src: /etc/sudoers
dest: /etc/sudoers.edit
remote_src: yes
validate: /usr/sbin/visudo -cf %s

# Create a CSV file from your complete inventory using an inline template
- hosts: all
tasks:
- copy:
content: |
HOSTNAME;IPADDRESS;FQDN;OSNAME;OSVERSION;PROCESSOR;ARCHITECTURE;MEMORY;
{\% for host in hostvars \%}
{\% set vars = hostvars[host|string] \%}
{\{ vars.ansible_hostname }\};{\{ vars.remote_host }\};{\{ vars.ansible_fqdn }\};{\{ vars.ansible_distribution }\};{\{ vars.ansible_distribution_version }\};{\{ vars.ansible_processor[1] }\};{\{ vars.ansible_architecture }\};{\{ (vars.ansible_memtotal_mb/1024)|round|int }\}; # NOQA
{\% endfor \%}
dest: /some/path/systems.csv
backup: yes
run_once: yes
delegate_to: localhost

fetch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Store file into /tmp/fetched/host.example.com/tmp/somefile
- fetch:
src: /tmp/somefile
dest: /tmp/fetched

# Specifying a path directly
- fetch:
src: /tmp/somefile
dest: /tmp/prefix-{\{ inventory_hostname }\}
flat: yes

# Specifying a destination path
- fetch:
src: /tmp/uniquefile
dest: /tmp/special/
flat: yes

# Storing in a path relative to the playbook
- fetch:
src: /tmp/uniquefile
dest: special/prefix-{\{ inventory_hostname }\}
flat: yes

file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# change file ownership, group and mode. When specifying mode using octal numbers, first digit should always be 0.
- file:
path: /etc/foo.conf
owner: foo
group: foo
mode: 0644
- file:
src: /file/to/link/to
dest: /path/to/symlink
owner: foo
group: foo
state: link
- file:
src: '/tmp/{\{ item.src }\}'
dest: '{\{ item.dest }\}'
state: link
with_items:
- { src: 'x', dest: 'y' }
- { src: 'z', dest: 'k' }

# touch a file, using symbolic modes to set the permissions (equivalent to 0644)
- file:
path: /etc/foo.conf
state: touch
mode: "u=rw,g=r,o=r"

# touch the same file, but add/remove some permissions
- file:
path: /etc/foo.conf
state: touch
mode: "u+rw,g-wx,o-rwx"

# create a directory if it doesn't exist
- file:
path: /etc/some_directory
state: directory
mode: 0755

find

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
- name: Recursively find /tmp files older than 2 days
find:
paths: /tmp
age: 2d
recurse: yes

- name: Recursively find /tmp files older than 4 weeks and equal or greater than 1 megabyte
- find:
paths: /tmp
age: 4w
size: 1m
recurse: yes

- name: Recursively find /var/tmp files with last access time greater than 3600 seconds
- find:
paths: /var/tmp
age: 3600
age_stamp: atime
recurse: yes

- name: Find /var/log files equal or greater than 10 megabytes ending with .old or .log.gz
- find:
paths: /var/log
patterns: '*.old,*.log.gz'
size: 10m

# Note that YAML double quotes require escaping backslashes but yaml single quotes do not.
- name: Find /var/log files equal or greater than 10 megabytes ending with .old or .log.gz via regex
- find:
paths: /var/log
patterns: "^.*?\\.(?:old|log\\.gz)$"
size: 10m
use_regex: yes

ini_file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Before 2.3, option 'dest' was used instead of 'path'
- name: Ensure "fav=lemonade is in section "[drinks]" in specified file
ini_file:
path: /etc/conf
section: drinks
option: fav
value: lemonade
mode: 0600
backup: yes

- ini_file:
path: /etc/anotherconf
section: drinks
option: temperature
value: cold
backup: yes

iso_extract

1
2
3
4
5
6
7
- name: Extract kernel and ramdisk from a LiveCD
iso_extract:
image: /tmp/rear-test.iso
dest: /tmp/virt-rear/
files:
- isolinux/kernel
- isolinux/initrd.cgz

lineinfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
- lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: 'SELINUX=enforcing'

- lineinfile:
path: /etc/sudoers
state: absent
regexp: '^%wheel'

- lineinfile:
path: /etc/hosts
regexp: '^127\.0\.0\.1'
line: '127.0.0.1 localhost'
owner: root
group: root
mode: 0644

- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen '
insertafter: '^#Listen '
line: 'Listen 8080'

- lineinfile:
path: /etc/services
regexp: '^# port for http'
insertbefore: '^www.*80/tcp'
line: '# port for http by default'

# Add a line to a file if it does not exist, without passing regexp
- lineinfile:
path: /tmp/testfile
line: '192.168.1.99 foo.lab.net foo'

# Fully quoted because of the ': ' on the line. See the Gotchas in the YAML docs.
- lineinfile:
path: /etc/sudoers
state: present
regexp: '^%wheel\s'
line: '%wheel ALL=(ALL) NOPASSWD: ALL'

# Yaml requires escaping backslashes in double quotes but not in single quotes
- lineinfile:
path: /opt/jboss-as/bin/standalone.conf
regexp: '^(.*)Xms(\\d+)m(.*)$'
line: '\1Xms${xms}m\3'
backrefs: yes

# Validate the sudoers file before saving
- lineinfile:
path: /etc/sudoers
state: present
regexp: '^%ADMIN ALL='
line: '%ADMIN ALL=(ALL) NOPASSWD: ALL'
validate: '/usr/sbin/visudo -cf %s'

patch

1
2
3
4
5
6
7
8
9
10
- name: Apply patch to one file
patch:
src: /tmp/index.html.patch
dest: /var/www/index.html

- name: Apply patch to multiple files under basedir
patch:
src: /tmp/customize.patch
basedir: /var/www
strip: 1

replace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
- replace:
path: /etc/hosts
regexp: '(\s+)old\.host\.name(\s+.*)?$'
replace: '\1new.host.name\2'
backup: yes

# Replace after the expression till the end of the file (requires >=2.4)
- replace:
path: /etc/hosts
regexp: '(\s+)old\.host\.name(\s+.*)?$'
replace: '\1new.host.name\2'
after: 'Start after line.*'
backup: yes

# Replace before the expression till the begin of the file (requires >=2.4)
- replace:
path: /etc/hosts
regexp: '(\s+)old\.host\.name(\s+.*)?$'
replace: '\1new.host.name\2'
before: 'Start before line.*'
backup: yes

# Replace between the expressions (requires >=2.4)
- replace:
path: /etc/hosts
regexp: '(\s+)old\.host\.name(\s+.*)?$'
replace: '\1new.host.name\2'
after: 'Start after line.*'
before: 'Start before line.*'
backup: yes

- replace:
path: /home/jdoe/.ssh/known_hosts
regexp: '^old\.host\.name[^\n]*\n'
owner: jdoe
group: jdoe
mode: 0644

- replace:
path: /etc/apache/ports
regexp: '^(NameVirtualHost|Listen)\s+80\s*$'
replace: '\1 127.0.0.1:8080'
validate: '/usr/sbin/apache2ctl -f %s -t'

- name: short form task (in ansible 2+) necessitates backslash-escaped sequences
replace: dest=/etc/hosts regexp='\\b(localhost)(\\d*)\\b' replace='\\1\\2.localdomain\\2 \\1\\2'

- name: long form task does not
replace:
dest: /etc/hosts
regexp: '\b(localhost)(\d*)\b'
replace: '\1\2.localdomain\2 \1\2'

stat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Obtain the stats of /etc/foo.conf, and check that the file still belongs
# to 'root'. Fail otherwise.
- stat:
path: /etc/foo.conf
register: st
- fail:
msg: "Whoops! file ownership has changed"
when: st.stat.pw_name != 'root'

# Determine if a path exists and is a symlink. Note that if the path does
# not exist, and we test sym.stat.islnk, it will fail with an error. So
# therefore, we must test whether it is defined.
# Run this to understand the structure, the skipped ones do not pass the
# check performed by 'when'
- stat:
path: /path/to/something
register: sym

- debug:
msg: "islnk isn't defined (path doesn't exist)"
when: sym.stat.islnk is not defined

- debug:
msg: "islnk is defined (path must exist)"
when: sym.stat.islnk is defined

- debug:
msg: "Path exists and is a symlink"
when: sym.stat.islnk is defined and sym.stat.islnk

- debug:
msg: "Path exists and isn't a symlink"
when: sym.stat.islnk is defined and sym.stat.islnk == False


# Determine if a path exists and is a directory. Note that we need to test
# both that p.stat.isdir actually exists, and also that it's set to true.
- stat:
path: /path/to/something
register: p
- debug:
msg: "Path exists and is a directory"
when: p.stat.isdir is defined and p.stat.isdir

# Don't do md5 checksum
- stat:
path: /path/to/myhugefile
get_md5: no

# Use sha256 to calculate checksum
- stat:
path: /path/to/something
checksum_algorithm: sha256

synchronize

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Synchronization of src on the control machine to dest on the remote hosts
- synchronize:
src: some/relative/path
dest: /some/absolute/path

# Synchronization using rsync protocol (push)
- synchronize:
src: some/relative/path/
dest: rsync://somehost.com/path/

# Synchronization using rsync protocol (pull)
- synchronize:
mode: pull
src: rsync://somehost.com/path/
dest: /some/absolute/path/

# Synchronization using rsync protocol on delegate host (push)
- synchronize:
src: /some/absolute/path/
dest: rsync://somehost.com/path/
delegate_to: delegate.host

# Synchronization using rsync protocol on delegate host (pull)
- synchronize:
mode: pull
src: rsync://somehost.com/path/
dest: /some/absolute/path/
delegate_to: delegate.host

# Synchronization without any --archive options enabled
- synchronize:
src: some/relative/path
dest: /some/absolute/path
archive: no

# Synchronization with --archive options enabled except for --recursive
- synchronize:
src: some/relative/path
dest: /some/absolute/path
recursive: no

# Synchronization with --archive options enabled except for --times, with --checksum option enabled
- synchronize:
src: some/relative/path
dest: /some/absolute/path
checksum: yes
times: no

# Synchronization without --archive options enabled except use --links
- synchronize:
src: some/relative/path
dest: /some/absolute/path
archive: no
links: yes

# Synchronization of two paths both on the control machine
- synchronize:
src: some/relative/path
dest: /some/absolute/path
delegate_to: localhost

# Synchronization of src on the inventory host to the dest on the localhost in pull mode
- synchronize:
mode: pull
src: some/relative/path
dest: /some/absolute/path

# Synchronization of src on delegate host to dest on the current inventory host.
- synchronize:
src: /first/absolute/path
dest: /second/absolute/path
delegate_to: delegate.host

# Synchronize two directories on one remote host.
- synchronize:
src: /first/absolute/path
dest: /second/absolute/path
delegate_to: "{\{ inventory_hostname }\}"

# Synchronize and delete files in dest on the remote host that are not found in src of localhost.
- synchronize:
src: some/relative/path
dest: /some/absolute/path
delete: yes
recursive: yes

# Synchronize using an alternate rsync command
# This specific command is granted su privileges on the destination
- synchronize:
src: some/relative/path
dest: /some/absolute/path
rsync_path: "su -c rsync"

# Example .rsync-filter file in the source directory
# - var # exclude any path whose last part is 'var'
# - /var # exclude any path starting with 'var' starting at the source directory
# + /var/conf # include /var/conf even though it was previously excluded

# Synchronize passing in extra rsync options
- synchronize:
src: /tmp/helloworld
dest: /var/www/helloworld
rsync_opts:
- "--no-motd"
- "--exclude=.git"

tempfile

1
2
3
4
5
6
7
8
9
- name: create temporary build directory
tempfile:
state: directory
suffix: build

- name: create temporary file
tempfile:
state: file
suffix: temp

template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Example from Ansible Playbooks
- template:
src: /mytemplates/foo.j2
dest: /etc/file.conf
owner: bin
group: wheel
mode: 0644

# The same example, but using symbolic modes equivalent to 0644
- template:
src: /mytemplates/foo.j2
dest: /etc/file.conf
owner: bin
group: wheel
mode: "u=rw,g=r,o=r"

# Create a DOS-style text file from a template
- template:
src: config.ini.j2
dest: /share/windows/config.ini
newline_sequence: '\r\n'

# Copy a new "sudoers" file into place, after passing validation with visudo
- template:
src: /mine/sudoers
dest: /etc/sudoers
validate: '/usr/sbin/visudo -cf %s'

# Update sshd configuration safely, avoid locking yourself out
- template:
src: etc/ssh/sshd_config.j2
dest: /etc/ssh/sshd_config
owner: root
group: root
mode: '0600'
validate: /usr/sbin/sshd -t -f %s
backup: yes

unarchive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- name: Extract foo.tgz into /var/lib/foo
unarchive:
src: foo.tgz
dest: /var/lib/foo

- name: Unarchive a file that is already on the remote machine
unarchive:
src: /tmp/foo.zip
dest: /usr/local/bin
remote_src: 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

xattr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Obtain the extended attributes  of /etc/foo.conf
- xattr:
path: /etc/foo.conf

# Sets the key 'foo' to value 'bar'
- xattr:
path: /etc/foo.conf
key: user.foo
value: bar

# Removes the key 'foo'
- xattr:
path: /etc/foo.conf
key: user.foo
state: absent

xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
- name: Remove the subjective attribute of the rating element
xml:
path: /foo/bar.xml
xpath: /business/rating/@subjective
state: absent

- name: Set the rating to 11
xml:
path: /foo/bar.xml
xpath: /business/rating
value: 11

# Retrieve and display the number of nodes
- name: Get count of beers nodes
xml:
path: /foo/bar.xml
xpath: /business/beers/beer
count: yes
register: hits

- debug:
var: hits.count

- name: Add a phonenumber element to the business element
xml:
path: /foo/bar.xml
xpath: /business/phonenumber
value: 555-555-1234

- name: Add several more beers to the beers element
xml:
path: /foo/bar.xml
xpath: /business/beers
add_children:
- beer: Old Rasputin
- beer: Old Motor Oil
- beer: Old Curmudgeon

- name: Add a validxhtml element to the website element
xml:
path: /foo/bar.xml
xpath: /business/website/validxhtml

- name: Add an empty validatedon attribute to the validxhtml element
xml:
path: /foo/bar.xml
xpath: /business/website/validxhtml/@validatedon

- name: Add or modify an attribute, add element if needed
xml:
path: /foo/bar.xml
xpath: /business/website/validxhtml
attribute: validatedon
value: 1976-08-05

# How to read an attrribute value and access it in Ansible
- name: Read attribute value
xml:
path: /foo/bar.xml
xpath: /business/website/validxhtml
content: attribute
attribute: validatedon
register: xmlresp

- name: Show attribute value
debug:
var: xmlresp.matches[0].validxhtml.validatedon

- name: Remove all children from the website element (option 1)
xml:
path: /foo/bar.xml
xpath: /business/website/*
state: absent

- name: Remove all children from the website element (option 2)
xml:
path: /foo/bar.xml
xpath: /business/website
children: []

# In case of namespaces, like in below XML, they have to be explicitely stated
# NOTE: there's the prefix "x" in front of the "bar", too
#<?xml version='1.0' encoding='UTF-8'?>
#<foo xmlns="http://x.test" xmlns:attr="http://z.test">
# <bar>
# <baz xmlns="http://y.test" attr:my_namespaced_attribute="true" />
# </bar>
#</foo>

- name: Set namespaced '/x:foo/x:bar/y:baz/@z:my_namespaced_attribute' to 'false'
xml:
path: foo.xml
xpath: /x:foo/x:bar/y:baz
namespaces:
x: http://x.test
y: http://y.test
z: http://z.test
attribute: z:my_namespaced_attribute
value: 'false'

更多详情参考官网

版权所有,如有侵权请联系我