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


忽略任务失败
默认情况下,任务失败时play会中止。不过,可以通过忽略失败的任务来覆盖此行为。可以在任务中使用ignore_errors关键字来实现此目的

---
- hosts: 192.168.194.139
  tasks:
   - name: rojeo
     yum:
       name: elkhcid
       state: present
     ignore_errors: yes
   - name: ghcy
     command: echo 'yecvd'

[root@localhost ~]# ansible-playbook l.yml

PLAY [192.168.194.139] *********************************************************

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

TASK [rojeo] *******************************************************************
fatal: [192.168.194.139]: FAILED! => {"changed": false, "msg": "No package matching 'elkhcid' found available, installed or updated", "rc": 126, "results": ["No package matching 'elkhcid' found available, installed or updated"]}
...ignoring

TASK [ghcy] ********************************************************************
changed: [192.168.194.139]

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

常用文件模块

模块名称模块说明
blockinfile插入,更新或删除由可自定义标记线包围的多行文本块
copy将文件从本地或远程计算机复制到收管主机的位置,类似于file模块 ,
fetch此模块的作用和copy模块类似,但以相反方式工作。此模块用于从远程计算机获取文件到控制节点,并将它们存储在按主机名组织的文件树中
file设置权限、所有权、SELinux上下文以及常规文件、符号链接、硬链接和目录的时间戳等属性。此模块还可以创建或删除常规文件、符号链接、硬链接和目录。其他多个与文件相关的模块支持与file模块相同的属性设置选项,包括copy模块。
lineinfile确保特定行位于莫文件中,或使用返
stat检索文件的状态信息,
synchronize围绕rsync的一个打包程序,可加简化常见任务sysnchronize模块无法提供

blockinfile 模块

[root@localhost ~]# vim y.yml
---
- hosts: 192.168.194.139
  tasks:
    - name: fugvduvf
      blockinfile:
        path: /tmp/abc
        block: |
         first line.
         second line.
[root@localhost ~]# ansible-playbook  y.yml

PLAY [192.168.194.139] *************************************************************************************************************************************************

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

TASK [fugvduvf] ********************************************************************************************************************************************************
ok: [192.168.194.139]

PLAY RECAP *************************************************************************************************************************************************************
192.168.194.139            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
[root@localhost ~]# cat /tmp/abc 
# BEGIN ANSIBLE MANAGED BLOCK
first line.
second line.   
# END ANSIBLE MANAGED BLOCK[root@localhost ~]# 


file 模块

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

[root@localhost ~]# vim ll.yml 
---
- hosts: 192.168.194.139
  tasks:
    - name: Chang file ownership,group and permissions
      file:
         path: /etc/foo.conf
         owner: foo
         group: foo
         mode: '0644'
         state: touch                   
[root@localhost ~]# ansible-playbook ll.yml 
PLAY [192.168.194.139] *********************************************************

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

TASK [Chang file ownership,group and permissions] ******************************
changed: [192.168.194.139]

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

[root@localhost ~]# useradd foo
[root@localhost ~]# ll /etc/foo.conf 
-rw-r--r--. 1 foo foo 0 Sep  8 04:34 /etc/foo.conf
[root@localhost ~]# 
---
- hosts: 192.168.194.139
  tasks:
    - name: Chang file ownership,group and permissions
      file:
         path: /etc/foo.conf
         owner: root
         group: root
         mode: '0666'                       
         state: touch
[root@localhost ~]# ansible-playbook ll.yml 
PLAY [192.168.194.139] *********************************************************

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

TASK [Chang file ownership,group and permissions] ******************************
changed: [192.168.194.139]

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


[root@localhost ~]# ll /etc/foo.conf 
-rw-rw-rw-. 1 root root 0 Sep  8 04:46 /etc/foo.conf


copy模块受管主机上复制和编辑文件
[root@localhost ~]# vim ll.yml
---
- hosts: 192.168.194.139
  tasks:
     - name: ww
       copy:
          src: /etc/ABC
          dest: /opt/
          
[root@localhost ~]# ansible-playbook ll.yml 
PLAY [192.168.194.139] *********************************************************

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

TASK [ww] **********************************************************************
changed: [192.168.194.139]

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

 [root@localhost ~]# ls /opt/
ABC
                 

fetch模块


[root@localhost ~]# vim ll.yml 
---
- hosts: 192.168.194.139
  tasks:
  - name: akahfhudjal
    fetch:
      src: /etc/ABC
      dest: /tmp/
[root@localhost ~]# ansible-playbook ll.yml 
PLAY [192.168.194.139] *********************************************************

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

TASK [akahfhudjal] *************************************************************
changed: [192.168.194.139]

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

        
[root@localhost ~]# ls /etc/ABC
/etc/ABC
[root@localhost ~]# 
使用 file模块在从受管主机中删除文件
[root@localhost ~]# vim ll.yml 
---
- hosts: 192.168.194.139
  tasks:
     - name: Delete abc
       file:
         dest: /etc/abc
         state: absent

[root@localhost ~]# ansible-playbook ll.yml 
PLAY [192.168.194.139] *********************************************************

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

TASK [Delete abc] **************************************************************
changed: [192.168.194.139]

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

[root@localhost ~]# ls /etc/abc 
ls: cannot access /etc/abc: No such file or directory
[root@localhost ~]# 

stat模块

检测受管主机上的文件状态
检测文件的MD5校验和

[root@localhost ~]# vim ll.yml 
---
- hosts: 192.168.194.139
  tasks:
     - name: ghefhuig
       stat:
         path: /root/anaconda-ks.cfg
         checksum_algorithm: md5
       register: result
     - debug:
          var: result                    

[root@localhost ~]# ansible-playbook ll.yml 
PLAY [192.168.194.139] *********************************************************

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

TASK [ghefhuig] ****************************************************************
ok: [192.168.194.139]

TASK [debug] *******************************************************************
ok: [192.168.194.139] => {
    "result": {
        "changed": false,
        "failed": false,
        "stat": {
            "atime": 1598882528.3919017,
            "attr_flags": "",
            "attributes": [],
            "block_size": 4096,
            "blocks": 8,
            "charset": "us-ascii",
            "checksum": "5c3ad82c1e3c52bb26028668057e1cf7",
            "ctime": 1598882528.4299014,
            "dev": 64768,
            "device_type": 0,
            "executable": false,
            "exists": true,
            "gid": 0,
            "gr_name": "root",
            "inode": 33574979,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mimetype": "text/plain",
            "mode": "0600",
            "mtime": 1598882528.4299014,
            "nlink": 1,
            "path": "/root/anaconda-ks.cfg",
            "pw_name": "root",
            "readable": true,
            "rgrp": false,
            "roth": false,
            "rusr": true,
            "size": 1244,
            "uid": 0,
            "version": "18446744073177837059",
            "wgrp": false,
            "woth": false,
            "writeable": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    }
}

PLAY RECAP *********************************************************************
192.168.194.139            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
synchronize模块 同步控制节点和受管主机之间的文件

主控机和被控机都需要安装rsync

[root@localhost ~]# rpm -ivh /mnt/BaseOS/Packages/rsync-3.1.3-7.el8.x86_64.rpm 
warning: /mnt/BaseOS/Packages/rsync-3.1.3-7.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:rsync-3.1.3-7.el8                ################################# [100%]

[root@localhost ~]# rpm -qa|grep rsync
rsync-3.1.3-7.el8.x86_64
[root@localhost ~]# ansible-playbook  ll.yml 

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.194.139]
ok: [192.168.194.138]

TASK [install] *****************************************************************
ok: [192.168.194.138]
ok: [192.168.194.139]

TASK [synchroninze] ************************************************************
changed: [192.168.194.138]
changed: [192.168.194.139]

PLAY RECAP *********************************************************************
192.168.194.138            : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.194.139            : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
[root@localhost ~]#c vim ll.yml 
---
- hosts: 192.168.194.139
  tasks:
      name: fjglikre
      synchronize:
         src: /root/abc
         dest: /opt/
         
[root@localhost ~]# ls /opt/abc/
bcjdv       bcjdvfjhnvdnvjrd
bcjdvfjhnv  bcjdvfjhnvdnvjrdkjfcke

ansible使用jinja2管理配置文件以及jinja2语法简介

使用jinja2模板部署自定义文件
使用循环
jinja2使用for语句来提供循环功能。在下例中,user变量替换为users变量中包含的所有值,一行一个值。

[root@localhost ~]# vim files/pl.yml

---- 
hosts: 192.168.194.139
  tasks:
   - name: files/hosts.j2
     template:
       src: files/hosts.j2
       dest: /etc/hosts

使用循环

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

[root@localhost ~]# vim /etc/ansible/ansible.cfg 

[root@localhost ~]# vim files/user.j2 
{% for user in users %}
  {{  user }}
{% endfor %}

部署jinja2模板

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

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

[root@localhost ~]# vim playbook.yml

---
- hosts: 192.168.194.139
  vars:
     users:
      - hehe
      - xixi
      - lisi
  tasks:
     - name: get user
       template:
          src: files/user.j2
          dest: /tmp/user

[root@localhost ~]# ansible-playbook playbook.yml 

PLAY [192.168.194.139] *********************************************************

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

TASK [get user] ****************************************************************
changed: [192.168.194.139]

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

[root@localhost ~]# cat /tmp/user 
  hehe
  xixi
  lisi
[root@localhost ~]# 

变量过滤器

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

[root@localhost ~]# vim files/us.j2 
{{  ansible_facts ['python'] | to_json}}

[root@localhost ~]# vim playbook.yml 
---
- hosts: 192.168.194.139
  tasks:
     - name: get user
       template:
               src: files/us.j2
               dest: /tmp/

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

[root@localhost ~]# vim files/us.j2 

{{  ansible_facts ['python'] | to_yaml }}

[root@localhost ~]# vim playbook.yml 

---
- hosts: 192.168.194.139
  tasks:
     - name: get user
       template:
               src: files/us.j2
               dest: /tmp/

 [root@localhost ~]# cat /tmp/us.j2 
{"executable": "/usr/bin/python", "version": {"micro": 5, "major": 2, "releaselevel": "final", "serial": 0, "minor": 7}, "type": "CPython", "has_sslcontext": true, "version_info": [2, 7, 5, "final", 0]}

[root@localhost ~]# vim files/us.j2 

{{ '{"name": "c","jgjk":28,"djhvufh": "dhc"}' | from_json }}

[root@localhost ~]# cat /tmp/us.j2 
{'name': 'c', 'jgjk': 28, 'djhvufh': 'dhc'}

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

[root@localhost ~]# vim files/us.j2 

{{  ansible_facts ['python'] | to_nice_yam l}}
[root@localhost ~]# cat /tmp/us.j2 
executable: /usr/bin/python
has_sslcontext: true
type: CPython
version:
    major: 2
    micro: 5
    minor: 7
    releaselevel: final
    serial: 0
version_info:
- 2
- 7
- 5
- final
- 0


[root@localhost ~]# vim files/us.j2 

{{  ansible_facts ['python'] | to_nice_json }}

[root@localhost ~]# cat /tmp/us.j2 
{
    "executable": "/usr/bin/python",
    "has_sslcontext": true,
    "type": "CPython",
    "version": {
        "major": 2,
        "micro": 5,
        "minor": 7,
        "releaselevel": "final",
        "serial": 0
    },
    "version_info": [
        2,
        7,
        5,
        "final",
        0
    ]
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值