ansible基础用法8_block and line

blockinfile模块可以在指定的文件中插入一段文本,这段文本是被标记过的,以便在以后的操作中可以通过标记找到这段文本,然后修改或者删除这段文本

lineinfile模块可以确保某一行文本存在于指定的文件中,或者确保从文件中删除指定的文本,还可以根据正则表达式替换某一行文本

其实两则没有本质的区别,line 针对某行,block 是某个段或者某几行

 

---
- hosts: all

  tasks:
    - name: judge file is exits
      stat: path=/opt/block.txt
      ignore_errors: True
      register: file_result

    - debug:
        msg: "the block.txt not exists"
      when: file_result.stat.exists != True
    - debug:
        msg: "the block.txt is exists"
      when: file_result.stat.exists != False


    - name: the block.txt is not exists and create the file
      file: path=/opt/block.txt state=touch mode=0777
      when: file_result.stat.exists != True


    - name: cat /opt/block.txt
      shell : cat /opt/block.txt
      register: result
    - debug:
        msg: "{{ result.stdout_lines }}"
      ignore_errors: yes

    - name: delete the block1
      lineinfile:
        dest: /opt/block.txt
        regexp: '^{{item}}'
        state: absent
      with_items:
        - b*
        - "ANSIBLE INJECTED  NODE SSH FOR"



    - name: insert lineinfile in file
      blockinfile:
        dest: /opt/block.txt
        state: present
        marker: "ANSIBLE INJECTED  NODE SSH FOR"
        block: |
          block1
          block2
          block3
#      run_once: yes

 

同样 lineinfile 也能达到相同的效果

---
- hosts: all
  tasks:
    - name: line more line
      lineinfile:
        dest: /opt/block.txt
        regexp: line1
        line: |
          line3
          line4

结果输出

[root@ansible AnsibleInstallRpm]# ansible-playbook ansible_base8.yml 

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

TASK [setup] *******************************************************************
ok: [node2]
ok: [node1]

TASK [line more line] **********************************************************
changed: [node2]
changed: [node1]

PLAY RECAP *********************************************************************
node1                      : ok=2    changed=1    unreachable=0    failed=0   
node2                      : ok=2    changed=1    unreachable=0    failed=0   

[root@ansible AnsibleInstallRpm]# ansible all -m shell -a "cat /opt/block.txt"
node2 | SUCCESS | rc=0 >>
ANSIBLE INJECTED  NODE SSH FOR
block1
block2
block3
ANSIBLE INJECTED  NODE SSH FOR
line3
line4

node1 | SUCCESS | rc=0 >>
ANSIBLE INJECTED  NODE SSH FOR
block1
block2
block3
ANSIBLE INJECTED  NODE SSH FOR
line3
line4

 

TASK [apex : DEBUG - Confirm re_update.yaml is running] ************************************************************************************************************************************************* ok: [10.11.107.89] => { "msg": "re_update.yaml is definitely running!" } TASK [apex : Set type to 'jd' if dist_id is between 7000-7999] ****************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Set tmp type to 'jd'] ********************************************************************************************************************************************************************** ok: [10.11.107.89] => {"ansible_facts": {"type": "jd"}, "changed": false} TASK [apex : Check each server's ax_update directory status] ******************************************************************************************************************************************** ok: [10.11.107.89] => (item={'ip': '47.94.174.254', 'port_num': '9010', 'line_num': '1', 'dist_id': '10000', 'line_num_cn': '??'}) => {"ansible_loop_var": "item", "changed": false, "examined": 0, "files": [], "item": {"dist_id": "10000", "ip": "47.94.174.254", "line_num": "1", "line_num_cn": "??", "port_num": "9010"}, "matched": 0, "msg": "All paths examined", "skipped_paths": {}} ok: [10.11.107.89] => (item={'ip': '47.94.174.254', 'port_num': '9012', 'line_num': '2', 'dist_id': '10000', 'line_num_cn': '??'}) => {"ansible_loop_var": "item", "changed": false, "examined": 0, "files": [], "item": {"dist_id": "10000", "ip": "47.94.174.254", "line_num": "2", "line_num_cn": "??", "port_num": "9012"}, "matched": 0, "msg": "All paths examined", "skipped_paths": {}} ok: [10.11.107.89] => (item={'ip': '47.94.174.254', 'port_num': '9014', 'line_num': '3', 'dist_id': '10000', 'line_num_cn': '??'}) => {"ansible_loop_var": "item", "changed": false, "examined": 0, "files": [], "item": {"dist_id": "10000", "ip": "47.94.174.254", "line_num": "3", "line_num_cn": "??", "port_num": "9014"}, "matched": 0, "msg": "All paths examined", "skipped_paths": {}} ok: [10.11.107.89] => (item={'ip': '47.94.174.254', 'port_num': '9016', 'line_num': '4', 'dist_id': '10000', 'line_num_cn': '??'}) => {"ansible_loop_var": "item", "changed": false, "examined": 0, "files": [], "item": {"dist_id": "10000", "ip": "47.94.174.254", "line_num": "4", "line_num_cn": "??", "port_num": "9016"}, "matched": 0, "msg": "All paths examined", "skipped_paths": {}} TASK [apex : Create proper server status list] ********************************************************************************************************************************************************** ok: [10.11.107.89] => {"ansible_facts": {"server_status": [{"empty": false, "exists": true, "line_num": 1, "path": []}, {"empty": false, "exists": true, "line_num": 2, "path": []}, {"empty": false, "exists": true, "line_num": 3, "path": []}, {"empty": false, "exists": true, "line_num": 4, "path": []}]}, "changed": false} TASK [apex : Print server_status] *********************************************************************************************************************************************************************** ok: [10.11.107.89] => { "server_status": [ { "empty": false, "exists": true, "line_num": 1, "path": [] }, { "empty": false, "exists": true, "line_num": 2, "path": [] }, { "empty": false, "exists": true, "line_num": 3, "path": [] }, { "empty": false, "exists": true, "line_num": 4, "path": [] } ] } TASK [apex : Ensure local directory exists for rsync] *************************************************************************************************************************************************** ok: [10.11.107.89] => {"changed": false, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "files/jd", "size": 4096, "state": "directory", "uid": 0} TASK [apex : Sync latest .zzz files from remote server] ************************************************************************************************************************************************* skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Parse and set latest patch file] *********************************************************************************************************************************************************** ok: [10.11.107.89] => {"ansible_facts": {"patch_path": false}, "changed": false} TASK [apex : Terminate if no update patch found] ******************************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Copy update pack only to empty directories] ************************************************************************************************************************************************ skipping: [10.11.107.89] => (item=svr1 (non-empty)) => {"ansible_loop_var": "item", "changed": false, "item": {"empty": false, "exists": true, "line_num": 1, "path": []}, "skip_reason": "Conditional result was False"} skipping: [10.11.107.89] => (item=svr2 (non-empty)) => {"ansible_loop_var": "item", "changed": false, "item": {"empty": false, "exists": true, "line_num": 2, "path": []}, "skip_reason": "Conditional result was False"} skipping: [10.11.107.89] => (item=svr3 (non-empty)) => {"ansible_loop_var": "item", "changed": false, "item": {"empty": false, "exists": true, "line_num": 3, "path": []}, "skip_reason": "Conditional result was False"} skipping: [10.11.107.89] => (item=svr4 (non-empty)) => {"ansible_loop_var": "item", "changed": false, "item": {"empty": false, "exists": true, "line_num": 4, "path": []}, "skip_reason": "Conditional result was False"} TASK [apex : Detect processes of GS] ******************************************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Terminate if no gs worker] ***************************************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Get worker state of port listening] ******************************************************************************************************************************************************** skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} TASK [apex : Wait async wait_for] *********************************************************************************************************************************************************************** skipping: [10.11.107.89] => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false} TASK [apex : Start apex worker] ************************************************************************************************************************************************************************* skipping: [10.11.107.89] => {"changed": false, "skip_reason": "Conditional result was False"} PLAY RECAP ********************************************************************************************************************************************************************************************** 10.11.107.89 : ok=13 changed=0 unreachable=0 failed=0 skipped=17 rescued=0 ignored=0 这个任务没有按预期执行,实际需求: 1.判断远程机器上{{ apex_home }}/svr{{ item.line_num }}/ax_update/目录下是否含有.zzz文件 2.如果不含则执行Sync latest .zzz files from remote server 和 Copy update pack only to empty directories 3.如果含有,则跳过执行 --- - name: DEBUG - Confirm re_update.yaml is running debug: msg: "re_update.yaml is definitely running!" - name: Set type to 'jd' if dist_id is between 7000-7999 ansible.builtin.set_fact: type: "jd" when: > dist_id is defined and (dist_id | int) >= 7000 and (dist_id | int) <= 7999 - name: Set tmp type to 'jd' ansible.builtin.set_fact: type: "jd" - name: Check each server's ax_update directory status ansible.builtin.find: path: "{{ apex_home }}/svr{{ item.line_num }}/ax_update/" recurse: no register: dir_status loop: "{{ lines }}" ignore_errors: yes # 兼容目录不存在的情况 changed_when: false - name: Create proper server status list ansible.builtin.set_fact: server_status: >- [{% for item in lines | zip(dir_status.results) %} { "line_num": {{ item.0.line_num }}, "path": {{ item.1.files | default([]) | to_json }}, "empty": {{ (item.1.files | default([]) | length) == 0 | to_json }}, "exists": {{ (item.1.files is defined) | to_json }} }{% if not loop.last %},{% endif %} {% endfor %}] - name: Print server_status ansible.builtin.debug: var: server_status - name: Ensure local directory exists for rsync ansible.builtin.file: path: "files/{{ type }}" state: directory mode: '0755' - name: Sync latest .zzz files from remote server ansible.builtin.command: > rsync -avz --port 50000 --include='*.zzz' --exclude='*' 10.14.101.113::apex_update/{{ type }}/ files/{{ type }}/ args: creates: "files/{{ type }}/" register: rsync_result changed_when: "'sent' in rsync_result.stdout" when: server_status | selectattr('empty') | list | length > 0 - name: Parse and set latest patch file ansible.builtin.set_fact: patch_path: "{{ query('filetree', 'files/' ~ type) | sort(attribute='mtime') | map(attribute='src') | last | default(false) }}" - name: Terminate if no update patch found ansible.builtin.fail: msg: "Update patch not found in files/{{ type }}/ after rsync" when: server_status | selectattr('empty') | list | length > 0 and not patch_path - name: Copy update pack only to empty directories ansible.builtin.copy: src: "{{ patch_path }}" dest: "{{ apex_home }}/svr{{ item.line_num }}/ax_update/" remote_src: false vars: patch_date: "{{ patch_path | regex_search(apex_date_re) }}" loop: "{{ server_status }}" when: - item.empty - not item.path | selectattr('path', 'contains', patch_date) | list loop_control: label: "svr{{ item.line_num }} ({{ 'empty' if item.empty else 'non-empty' }})"
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石兴稳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值