在被管理节点上创建文件或目录

修改文件并将其复制到主机

描述文件模块

Files模块库包含的模块允许用户完成与Linux文件管理相关的大多数任务,如创建、复制、编辑和修改文件的权限和其他属性。下表提供了常用文件管理模块的列表:

常用文件模块

模块名称模块说明
blockinfile插入、更新或删除由可自定义标记线包围的多行文本块
copy将文件从本地或远程计算机复制到受管主机上的某个位置。 类似于file模块,copy模块还可以设置文件属性,包括SELinux上下文件。
fetch此模块的作用和copy模块类似,但以相反方式工作。此模块用于从远程计算机获取文件到控制节点, 并将它们存储在按主机名组织的文件树中。
file设置权限、所有权、SELinux上下文以及常规文件、符号链接、硬链接和目录的时间戳等属性。 此模块还可以创建或删除常规文件、符号链接、硬链接和目录。其他多个与文件相关的 模块支持与file模块相同的属性设置选项,包括copy模块。
lineinfile确保特定行位于某文件中,或使用反向引用正则表达式来替换现有行。 此模块主要在用户想要更改文件的某一行时使用。
stat检索文件的状态信息,类似于Linux中的stat命令。
synchronize围绕rsync命令的一个打包程序,可加快和简化常见任务。 synchronize模块无法提供对rsync命令的完整功能的访问权限,但确实最常见的调用更容易实施。 用户可能仍需通过run command模块直接调用rsync命令。

files模块的自动化示例

在受管主机上创建、复制、编辑和删除文件是用户可以使用Files模块库中的模块实施的常见任务。

确保受管主机上存在文件

使用file模块处理受管主机上的文件。其工作方式与touch命令类似,如果不存在则创建一个空文件,如果存在,则更新其修改时间。在本例中,除了处理文件之外,Ansible还确保将文件的所有者、组和权限设置为特定值。

[root@localhost ansible]# cat test/aaa.yml 
---
- nmae: user
  hosts: 192.168.100.42
  tasks:
    - name: file
      file:
        psth: /opt/aaa
        owner: harry
        group: harry
        mode: 2012
        state: touch
[root@localhost ansible]# 

修改文件属性

使用file模块还可以确保新的或现有的文件具有正确的权限和SELinux类型。

例如,以下文件保留了相对于用户主目录的默认SELinux上下文,这不是所需的上下文。

[root@localhost ~]# ls -Z samba_file
- rw-r--r-- owner group unconfined_u:object_r:user_home_t:s0 samba_file

以下任务确保了anaconda-ks.cfg文件的SELinux上下文件类型属性是所需的samba_share_t类型。此行为与Linux中的chcon命令类似。

- name: SELinux type is set to samba_share_t
  file:
    path: /path/to/samba_file
    setype: samba_share_t

结果

[root@localhost ~]# ls -Z samba_file
- rw-r--r-- owner group unconfined_u:object_r:samba_share_t:s0 samba_file

使SELinux文件上下文更具持久性

设置文件上下文时,file模块的行为与chcon类似。通过运行restorecon,可能会意外地撤消使用该模块所做的更改。使用file设置上下文后,用户可以使用system模块集合中的sefcontext来更新SELinux策略,如semanage fcontext。

- name: SELinux type is persistently set to samba_share_t
  sefcontext:
    target: /path/to/samba_file
    setype: samba_share_t
    state: present

注意:sefcontext模块更新SELinux策略中目标的默认上下文,但不更改现有文件的上下文。

在受管主机上复制和编辑文件

在此示例中,copy模块用于将位于控制节点上的Ansible工作目录中的文件复制到选定的受管主机。

默认情况下,此模块假定设置了force: yes。这会强制该模块覆盖远程文件(如果存在但包含与正在复制的文件不同的内容)。如果设置force: no,则它仅会将该文件复制到受管主机(如果该文件尚不存在)。

--
- name: test
  hosts: 192.168.100.42
  tasks:
    - name: cp
      copy:
        src: /etc/ansible/test/aaa.yml
        dest: /opt/   

要从受管主机检索文件,请使用fetch模块。这可用于在将参考系统分发给其他受管主机之前从参考系统中检查诸如SSH公钥之类的文件。

---
- name: test
  hosts: "192.168.100.42"
  tasks:
    - name: fetch
      fetch:
        src: /opt/aaa
        dest: /etc/ansible/
        
[root@localhost ansible]# ls
192.168.100.42

[root@localhost ansible]# cd 192.168.100.42
[root@localhost 192.168.100.42]# ls
opt
[root@localhost 192.168.100.42]# cd opt/
[root@localhost opt]# ls
aaa

要确保现有文件中存在特定的单行文本,请使用lineinfile模块:

---
- name: user
  hosts: "192.168.100.42"
  tasks:
    - name: lineinfile
      lineinfile:            
         path: /etc/selinux/config  
         line: "SELINUX=disabled"  
         state: present  

要将文本块添加到现有文件,请使用blockinfile模块:

---
- name: user
  hosts: "*"
  tasks:
    - name: blockinfile 
      blockinfile:
        path: /etc/selinux/config
        block: |
                This is the first line
                This is the two line
        state: present  
 
 //在目标主机上查看结果       
# BEGIN ANSIBLE MANAGED BLOCK
This is the first line
This is the two line
# END ANSIBLE MANAGED BLOCK

注意:使用blockinfile模块时,注释块标记插入到块的开头和结尾,以确保幂等性

从受管主机中删除文件

从受管主机中删除文件的基本示例是使用file模块和state: absent参数。state参数对于许多模块是可选的。一些模块也支持其他选项。

[root@localhost ansible]# cat test/abc.yml 
---
- name: user
  hosts: 192.168.100.42
  tasks:
    - name: absent file
      file:
        path: /opt/aaa
        state: absent
[root@localhost ansible]# ansible-playbook test/abc.yml 

PLAY [user] ***********************************************************************

TASK [Gathering Facts] ************************************************************
ok: [192.168.100.42]

TASK [absent file] ****************************************************************
changed: [192.168.100.42]

PLAY RECAP ************************************************************************
192.168.100.42             : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]#

检索受管主机上的状态

stat模块检索文件的事实,类似于Linux中的stat命令。参数提供检索文件属性、确定文件检验和等功能。

stat模块返回一个包含文件状态数据的值的散列字典,允许用户使用单独的变量引用各条信息。

以下示例注册stat模块的结果,然后显示它检查的文件,有关stat模块返回的值的信息由ansible-doc记录,或者可以注册一个变量并显示其内容以查看可用内容:

[root@localhost ansible]# cat test/abc.yml 
---
- name: user
  hosts: 192.168.100.42
  tasks:
    - name: stat
      stat:
         path: /root/test.sh
      register: result
    - debug:
        var: result 
[root@localhost ansible]# ansible-playbook test/abc.yml 

PLAY [user] ***********************************************************************

TASK [Gathering Facts] ************************************************************
ok: [192.168.100.42]

TASK [stat] ***********************************************************************
ok: [192.168.100.42]

TASK [debug] **********************************************************************
ok: [192.168.100.42] => {
    "result": {
        "changed": false,
        "failed": false,
        "stat": {
            "exists": false
        }
    }
}

PLAY RECAP ************************************************************************
192.168.100.42             : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 

同步控制主机和受管主机之间的文件

synchronize模块是一个围绕rsync工具的打包程序,它简化了playbook中的常见文件管理任务。rsync工具必须同时安装在本机和远程主机上。默认情况下,在使用synchronize模块时,“本地主机”是同步任务所源自的主机(通常是控制节点),而“目标主机”是synchronize连接到的主机。

以下示例将位于Ansible工作目录中的文件同步到受管主机:

[root@localhost ansible]# cat test/abc.yml 
---
- name: user
  hosts: 192.168.100.42
  tasks:
    - name: sync
      synchronize:
         src: /etc/ansible/aaa
         dest: /opt/
[root@localhost ansible]# ansible-playbook test/abc.yml 

PLAY [user] ***********************************************************************

TASK [Gathering Facts] ************************************************************
ok: [192.168.100.42]

TASK [sync] ***********************************************************************
changed: [192.168.100.42]

PLAY RECAP ************************************************************************
192.168.100.42             : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 
[root@localhost opt]# ls   //在目标主机上查看
aaa  
[root@localhost opt]#

有很多种方法可以使用synchronize模块及其许多参数,包括同步目录。运行ansible-doc synchronize命令查看其他参数和playbook示例。

使用jinja2模板自定义文件

jianjia2简介

​ Ansible将jinja2模板系统用于模板文件。Ansible还使用jinja2语法来引用playbook中的变量。

变量和逻辑表达式置于标记或分隔符之间。例如,jinja2模板将**{% EXPR %}用于表达式或逻辑(如循环),而{{ EXPR }}则用于向最终用户输出表达式或变量的结果。后一标记在呈现时将被替换为一个或多个值,对最终用户可见。使用{# COMMENT #}**语法括起不应出现在最终文件中的注释。

在下例中,第一行中含有不会包含于最终文件中的注释。第二行中引用的变量被替换为所引用的系统事实的值。

[root@localhost ansible]# cat test/zxc.j2 
{# /etc/hosts line #}
{{ ansible_facts['default_ipv4']['address'] }}      {{ ansible_facts['hostname'] }}

[root@localhost ansible]# cat hosts.yml 
---
- name: test
  hosts: 192.168.100.42
  tasks: 
    - name: aaa
      template: 
        src: /etc/ansible/test/zxc.j2
        dest: /opt/abc
[root@localhost ansible]# 

[root@localhost opt]# cat abc    // 在目标主机上查看
192.168.100.42      localhost

构建jinja2模板

jinja2模板由多个元素组成:数据、变量和表达式。在呈现jinja2模板时,这些变量和表达式被替换为对应的值。模板中使用的变量可以在playbookvars部分中指定。可以将受管主机的事实用作模板中的变量。

​ 请记住,可以使用ansible system_hostname -i inventory_file -m setup命令来获取与受管主机相关的事实。

​ 下例演示了如何使用变量及Ansible从受管主机检索的事实创建**/etc/ssh/sshd_config的模板。当执行相关的playbook**时,任何事实都将被替换为所配置的受管主机中对应的值。

​ 注意:包含jinja2模板的文件不需要有任何特定的文件扩展名(如.j2)。但是,提供此类文件扩展名会让你更容易记住它是模板文件。

[root@localhost ansible]# cat test/zxc.j2 
{# /etc/hosts line #}
{{ ansible_facts['default_ipv4']['address'] }}      {{ ansible_facts['hostname'] }}

[root@localhost ansible]# cat hosts.yml 
---
- name: test
  hosts: 192.168.100.42
  tasks: 
    - name: aaa
      template: 
        src: /etc/ansible/test/zxc.j2
        dest: /opt/abc
[root@localhost ansible]# 
[root@localhost ansible]# ansible-playbook hosts.yml 

PLAY [test] **********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [192.168.100.42]

TASK [aaa] ***********************************************************************
ok: [192.168.100.42]

PLAY RECAP ***********************************************************************
192.168.100.42             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 
 
[root@localhost opt]# cat abc 
192.168.100.42      localhost

部署jinja2模块

jinja2模板是功能强大的工具,可用于自定义要在受管主机上部署的配置文件。创建了适用于配置文件的jinja2模板后,它可以通过template模板部署到受管主机上,该模块支持将控制节点中的本地文件转移到受管主机。

​ 若要使用template模块,请使用下列语法。与src键关联的值指定来源jinja2模板,而与dest键关联的值指定要在目标主机上创建的文件。

tasks:
  - name: template render
    template:
      src: /tmp/template.j2
      dest: /tmp/dest-config-file.txt

template模块还允许指定已部署文件的所有者、组、权限和SELINUX上下文,就像file模块一样。它也可以取用validate选项运行任意命令(如visudo -c),在将文件复制到位之前检查该文件的语法是否正确,有关更多详细信息,请参阅ansible-doc template

管理模板文件

为避免系统管理员修改Ansible部署的文件,最好在模板顶部包含注释,以指示不应手动编辑该文件。

可使用ansible_managed指令中设置的"Ansible managed"字符串来执行此操作。这不是正常变量,但可以在模板中用作一个变量。ansible_managed指令在ansible.cfg文件中设置:

root@localhost ansible]# vim ansible.cfg
148 # in some situations so the default is a static string:
149 ansible_managed = Aaaa bbbb
[root@localhost ansible]# cat test/zxc.j2 
{# /etc/hosts line #}
{{ ansible_managed }}   
{{ ansible_facts['default_ipv4']['address'] }}      {{ ansible_facts['hostname'] }}
[root@localhost ansible]# cat hosts.yml 
---
- name: test
  hosts: 192.168.100.42
  tasks: 
    - name: aaa
      template: 
        src: /etc/ansible/test/zxc.j2
        dest: /opt/abc
[root@localhost ansible]#

[root@localhost ~]# cat /opt/abc      
# This is a comment              
192.168.100.42      localhost

控制结构

​ 用户可以在模板文件中使用jinja2控制结构,以减少重复输入,为play中的每个主机动态输入条目,或者有条件地将文本插入到文件中。

使用循环

jinja2使用for语句来提供循环功能。在下例中,user变量替换为users变量中包含的所有值,一行一个值。

[root@localhost ansible]# cat test/zxc.j2 
{% for user in users %}
 {{ user }}
 ======
{% endfor %}
[root@localhost ansible]# cat test/qwe.yml 
users: 
  - aaa
  - bbb
  - ccc
[root@localhost ansible]# cat hosts.yml 
---
- name: test
  hosts: 192.168.100.42
  tasks: 
    - name: aaa
      template: 
        src: /etc/ansible/test/zxc.j2
        dest: /opt/abc
[root@localhost ansible]# 
[root@localhost ansible]# ansible-playbook hosts.yml 

PLAY [test] **********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [192.168.100.42]

TASK [aaa] ***********************************************************************
ok: [192.168.100.42]

PLAY RECAP ***********************************************************************
192.168.100.42             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 

[root@localhost ~]# cat /opt/abc 
  aaa
======
  bbb
======
  ccc
======

以下示例模板使用for语句逐一运行users变量中的所有值,将user替换为各个值,但值为wqy时除外。

[root@localhost ansible]# cat test/zxc.j2 
{% for user in users if not user == "aaa" %}     
 {{ user }}
=====
{% endfor %}
[root@localhost ansible]# ansible-playbook hosts.yml 

PLAY [test] **********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [192.168.100.42]

TASK [aaa] ***********************************************************************
ok: [192.168.100.42]

PLAY RECAP ***********************************************************************
192.168.100.42             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 
[root@localhost ~]# cat /opt/abc
  bbb
=====
  ccc
=====

使用 loop.index, loop.index变量扩展至循环当前所处的索引号。它在循环第一次执行时值为1,每一次迭代递增1。

[root@localhost ansible]#cat test/zxc.j2 
{% for user in users if not user == "wqy" %}
user number {{ loop.index }} -  {{ user }}
=====
{% endfor %}
[root@localhost ansible]# ansible-playbook hosts.yml 

PLAY [test] **********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [192.168.100.42]

TASK [aaa] ***********************************************************************
ok: [192.168.100.42]

PLAY RECAP ***********************************************************************
192.168.100.42             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 

[root@localhost ~]# cat /opt/abc
user number 1 -  bbb
=====
user number 2 -  ccc

使用条件语句

jinja2使用if语句来提供条件控制。如果满足某些条件,这允许用户在已部署的文件中放置一行。

在以下示例中,仅当**users变量的值为 **True 时,才可将结果的值放入已部署的文件。

[root@localhost ansible]#cat test/zxc.j2 
{%if users %}   
{{ users }}   
{% endif %}

[root@localhost ansible]# cat hosts.yml  
---
- name: jianja2
  hosts: 192.168.220.8
  vars_files:
    - vars/user.yml
  tasks:
    - name: fdfdf          
      template:
        src: /etc/ansible/jianjin.j2
        dest: /opt/abc
        
[root@localhost ansible]# ansible-playbook hosts.yml 

PLAY [test] **********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [192.168.100.42]

TASK [aaa] ***********************************************************************
ok: [192.168.100.42]

PLAY RECAP ***********************************************************************
192.168.100.42             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]#       
[root@localhost opt]# cat abc
['aaa', 'bbb', 'ccc']
[root@localhost opt]# 

注意,在Ansible模板中我们可以使用jinja2循环和条件,但不能在Ansible Playbook中使用。

变量过滤器

jinja2提供了过滤器,更改模板表达式的输出格式(例如,输出到效果JSON)。有适用于YAML和JSON等语言的过滤器。to_json过滤器使用JSON格式化表达式输出,to_yaml过滤器则使用YAML格式化表达式输出。

[root@localhost ansible]# cat test/zxc.j2 
{%if users %}
{{ users | to_json }}
{% endif %}
[root@localhost ansible]# ansible-playbook hosts.yml 

PLAY [test] **********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [192.168.100.42]

TASK [aaa] ***********************************************************************
ok: [192.168.100.42]

PLAY RECAP ***********************************************************************
192.168.100.42             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 

[root@localhost ~]# cat /opt/abc
["aaa", "bbb", "ccc"]

也有其他过滤器,如to_nice_jsonto_nice_yaml过滤器,它们将表达式输出格式化为JSON或YAML等人类可读格式。

[root@localhost ansible]# cat test/zxc.j2 
{%if users %}
{{ users | to_nice_json }}      
{% endif %}
[root@localhost ansible]# ansible-playbook hosts.yml 

PLAY [test] **********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [192.168.100.42]

TASK [aaa] ***********************************************************************
ok: [192.168.100.42]

PLAY RECAP ***********************************************************************
192.168.100.42             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 
[root@localhost ~]# cat /opt/abc 
[
    "aaa",
    "bbb",
    "ccc"
]

from_jsonfrom_yaml过滤器相应要求JSON或YAML格式的字符串,并对它们进行解析。

[root@localhost ansible]# cat test/zxc.j2 
{%if users %}
{{ users | from_yaml }} 
{% endif %}
[root@localhost ansible]# ansible-playbook hosts.yml 

PLAY [test] **********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [192.168.100.42]

TASK [aaa] ***********************************************************************
ok: [192.168.100.42]

PLAY RECAP ***********************************************************************
192.168.100.42             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 
[root@localhost ~]# cat /opt/abc  
['aaa', 'bbb', 'ccc']

变量测试

​ 在Ansible Playbook中与when子句一同使用的表达式是jinja2表达式。用于测试返回值的内置Ansible测试包括failedchangedsuccessdedskipped。以下任务演示了如何在条件表达式内使用测试

tasks:
...output omitted...
  - debug: msg="the execution was aborted"
    when: returnvalue is failed
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值