| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
# This is a virtual module that is entirely implemented as an action plugin and runs on the controller |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
--- |
| 13 |
module: template |
| 14 |
version_added: historical |
| 15 |
short_description: Template a file out to a target host |
| 16 |
options: |
| 17 |
follow: |
| 18 |
description: |
| 19 |
- Determine whether symbolic links should be followed. |
| 20 |
- When set to V(true) symbolic links will be followed, if they exist. |
| 21 |
- When set to V(false) symbolic links will not be followed. |
| 22 |
- Previous to Ansible 2.4, this was hardcoded as V(true). |
| 23 |
type: bool |
| 24 |
default: no |
| 25 |
version_added: '2.4' |
| 26 |
notes: |
| 27 |
- For Windows you can use M(ansible.windows.win_template) which uses V(\\r\\n) as O(newline_sequence) by default. |
| 28 |
- The C(jinja2_native) setting has no effect. Native types are never used in the M(ansible.builtin.template) module |
| 29 |
which is by design used for generating text files. For working with templates and utilizing Jinja2 native types see |
| 30 |
the O(ansible.builtin.template#lookup:jinja2_native) parameter of the P(ansible.builtin.template#lookup) lookup. |
| 31 |
seealso: |
| 32 |
- module: ansible.builtin.copy |
| 33 |
- module: ansible.windows.win_copy |
| 34 |
- module: ansible.windows.win_template |
| 35 |
author: |
| 36 |
- Ansible Core Team |
| 37 |
- Michael DeHaan |
| 38 |
extends_documentation_fragment: |
| 39 |
- action_common_attributes |
| 40 |
- action_common_attributes.flow |
| 41 |
- action_common_attributes.files |
| 42 |
- backup |
| 43 |
- files |
| 44 |
- template_common |
| 45 |
- validate |
| 46 |
attributes: |
| 47 |
action: |
| 48 |
support: full |
| 49 |
async: |
| 50 |
support: none |
| 51 |
bypass_host_loop: |
| 52 |
support: none |
| 53 |
check_mode: |
| 54 |
support: full |
| 55 |
diff_mode: |
| 56 |
support: full |
| 57 |
platform: |
| 58 |
platforms: posix |
| 59 |
safe_file_operations: |
| 60 |
support: full |
| 61 |
vault: |
| 62 |
support: full |
| 63 |
""" |
| 64 |
|
| 65 |
EXAMPLES = r""" |
| 66 |
- name: Template a file to /etc/file.conf |
| 67 |
ansible.builtin.template: |
| 68 |
src: /mytemplates/foo.j2 |
| 69 |
dest: /etc/file.conf |
| 70 |
owner: bin |
| 71 |
group: wheel |
| 72 |
mode: '0644' |
| 73 |
|
| 74 |
- name: Template a file, using symbolic modes (equivalent to 0644) |
| 75 |
ansible.builtin.template: |
| 76 |
src: /mytemplates/foo.j2 |
| 77 |
dest: /etc/file.conf |
| 78 |
owner: bin |
| 79 |
group: wheel |
| 80 |
mode: u=rw,g=r,o=r |
| 81 |
|
| 82 |
- name: Copy a version of named.conf that is dependent on the OS. setype obtained by doing ls -Z /etc/named.conf on original file |
| 83 |
ansible.builtin.template: |
| 84 |
src: named.conf_{{ ansible_os_family }}.j2 |
| 85 |
dest: /etc/named.conf |
| 86 |
group: named |
| 87 |
setype: named_conf_t |
| 88 |
mode: '0640' |
| 89 |
|
| 90 |
- name: Create a DOS-style text file from a template |
| 91 |
ansible.builtin.template: |
| 92 |
src: config.ini.j2 |
| 93 |
dest: /share/windows/config.ini |
| 94 |
newline_sequence: '\r\n' |
| 95 |
|
| 96 |
- name: Copy a new sudoers file into place, after passing validation with visudo |
| 97 |
ansible.builtin.template: |
| 98 |
src: /mine/sudoers |
| 99 |
dest: /etc/sudoers |
| 100 |
validate: /usr/sbin/visudo -cf %s |
| 101 |
|
| 102 |
- name: Update sshd configuration safely, avoid locking yourself out |
| 103 |
ansible.builtin.template: |
| 104 |
src: etc/ssh/sshd_config.j2 |
| 105 |
dest: /etc/ssh/sshd_config |
| 106 |
owner: root |
| 107 |
group: root |
| 108 |
mode: '0600' |
| 109 |
validate: /usr/sbin/sshd -t -f %s |
| 110 |
backup: yes |
| 111 |
""" |
| 112 |
|
| 113 |
RETURN = r""" |
| 114 |
dest: |
| 115 |
description: Destination file/path, equal to the value passed to I(dest). |
| 116 |
returned: success |
| 117 |
type: str |
| 118 |
sample: /path/to/file.txt |
| 119 |
checksum: |
| 120 |
description: SHA1 checksum of the rendered file |
| 121 |
returned: always |
| 122 |
type: str |
| 123 |
sample: 373296322247ab85d26d5d1257772757e7afd172 |
| 124 |
uid: |
| 125 |
description: Numeric id representing the file owner |
| 126 |
returned: success |
| 127 |
type: int |
| 128 |
sample: 1003 |
| 129 |
gid: |
| 130 |
description: Numeric id representing the group of the owner |
| 131 |
returned: success |
| 132 |
type: int |
| 133 |
sample: 1003 |
| 134 |
owner: |
| 135 |
description: User name of owner |
| 136 |
returned: success |
| 137 |
type: str |
| 138 |
sample: httpd |
| 139 |
group: |
| 140 |
description: Group name of owner |
| 141 |
returned: success |
| 142 |
type: str |
| 143 |
sample: www-data |
| 144 |
md5sum: |
| 145 |
description: MD5 checksum of the rendered file |
| 146 |
returned: changed |
| 147 |
type: str |
| 148 |
sample: d41d8cd98f00b204e9800998ecf8427e |
| 149 |
mode: |
| 150 |
description: Unix permissions of the file in octal representation as a string |
| 151 |
returned: success |
| 152 |
type: str |
| 153 |
sample: 1755 |
| 154 |
size: |
| 155 |
description: Size of the rendered file in bytes |
| 156 |
returned: success |
| 157 |
type: int |
| 158 |
sample: 42 |
| 159 |
src: |
| 160 |
description: Source file used for the copy on the target machine. |
| 161 |
returned: changed |
| 162 |
type: str |
| 163 |
sample: /home/httpd/.ansible/tmp/ansible-tmp-1423796390.97-147729857856000/source |
| 164 |
""" |