Ansible Files模块

file:设置文件属性

用于设置文件属性(权限、属主/组),同时也可用于创建或删除文件/目录,对文件/目录创建或删除软、硬链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- hosts: localhost
tasks:
- name: create directory
file: path="{{ item }}" state=directory mode=0750 group=admin
with_items:
- /data/test1
- /data/test2
- /data2
- name: touch a file
file: path=/data/test1/{{ item }} state=touch
with_items:
- test1
- test2
- name: create a soft link
file: path=/data/test2/test2 src=/data/test1/test1 state=link
- name: delete a file
file: path=/data/test1/test2 state=absent
1
2
3
4
5
6
# tree /data
/data
├── test1
│ └── test1
└── test2
└── test2 -> /data/test1/test1

fetch:从远程节点获取一个文件(非目录)

如果不指定flat参数,则默认会在目标加上源文件所在的hostname和绝对路径

1
2
3
4
5
6
- hosts: 10.1.10.71
tasks:
- name: "fetch a file from remote"
fetch: src="/etc/hosts" dest="/root/"
- name: "fetch a file from remote"
fetch: src="/etc/fstab" dest="/root/" flat=yes
1
2
3
4
5
6
# tree /root/
/root/
├── 10.1.10.71
│ └── etc
│ └── hosts
└── fstab

copy:从本地或远程拷贝一个文件或目录到远程(与fetch反向)

backup参数指定如果文件已存在且内容不一致则备份(否则直接覆盖)

1
2
3
4
5
6
7
8
9
10
11
12
13
- hosts: localhost
tasks:
- name: grant sudo to user1
copy:
content: |
user1 ALL=(ALL) NOPASSWD:ALL
user2 ALL=(ALL) NOPASSWD:ALL
dest: /etc/sudoers.d/user1
validate: '/usr/sbin/visudo -cf %s'
- name: Copy /etc/networks to /root/
copy: src=/etc/networks dest=/root/
- name: Copy /etc/issue to /root/networks,and backing up the original if it differs from the copied version
copy: src=/etc/issue dest=/root/networks owner=admin group=admin mode=755 backup=yes
1
2
3
4
5
6
7
8
9
10
11
12
13
# ll networks*
-rw-r--r-- 1 root root 47 Mar 17 01:00 networks
-rw-r--r-- 1 root root 58 Mar 17 01:00 networks.70457.2017-03-17@01:00:33~
# cat networks
CentOS release 6.8 (Final)
Kernel \r on an \m
# cat networks.70457.2017-03-17\@01\:00\:33~
default 0.0.0.0
loopback 127.0.0.0
link-local 169.254.0.0
# cat /etc/sudoers.d/user1
user1 ALL=(ALL) NOPASSWD:ALL
user2 ALL=(ALL) NOPASSWD:ALL

template:


ini_file:修改ini格式文件的配置

blockinfile:插入/修改/移除被marker标识的文本块

只作用于被marker(“# {mark} ANSIBLE MANAGED BLOCK”)所包围的文本块

1
2
3
4
5
6
7
8
9
10
- hosts: localhost
tasks:
- name: Add mappings to /etc/hosts
blockinfile:
path: /etc/hosts
block: |
192.168.1.1 host1
192.168.1.2 host2
192.168.1.3 host3
marker: "# {mark} ANSIBLE MANAGED BLOCK for test"
1
2
3
4
5
6
# cat /etc/hosts
# BEGIN ANSIBLE MANAGED BLOCK for test
192.168.1.1 host1
192.168.1.2 host2
192.168.1.3 host3
# END ANSIBLE MANAGED BLOCK for test

lineinfile:对文件中的特殊行,引用正则表达式进行替换(插入/修改/删除)

只能匹配单行(仅最后一次匹配行生效,多行可用replace)

参数 作用
regexp 用于匹配的正则表达式(只匹配最后一次出现的行)
insertbefore 用于在所匹配到的前一行插入line指定的文本
insertafter 用于在所匹配到的后一行插入line指定的文本
backrefs 若未匹配到则文件保持不变(默认值为no,则未匹配到将在末尾插入line指定的文本)
1
2
3
4
5
6
7
8
- hosts: localhost
tasks:
- name: Modify host mapping or insert if line is not exist
lineinfile: path=/etc/hosts regexp="^{{ ansible_eth0.ipv4.address }}" line="{{ ansible_eth0.ipv4.address }} ansible_100"
- name: Disable DNS resolv for ssh
lineinfile: path=/etc/ssh/sshd_config regexp=".*UseDNS" line="UseDNS no"
- name: Disable SELINUX
lineinfile: path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"
坚持原创技术分享,您的支持将鼓励我继续创作!
0%