blockinfile和lineinfile错误

问题描述 投票:-1回答:1

我正在尝试从路由器获取命令的输出并将其放入文本文件中,主机的IP地址作为文本文件的句柄。我不明白lineinfile和blockinfile都会发生一些事情。我希望有人能让我知道我做错了什么?

---
- name: Get 6860 light levels
  hosts: '10.20.46.4'
  gather_facts: no
  vars:
   optics_6860: 'show interfaces ddm'
  tasks:
   - name: Get 6860 Light levels
     raw: '{{ optics_6860 }}'
     register: command_output
   - name: Append to file
     blockinfile:
      create: yes
      path: '/home/testuser/ansible/output/{{ inventory_hostname }}.txt'
      block: |
       '{{ command_output.stdout }}'
(networking_env) [testuser@ToolBox ~]$ ansible-playbook 
ansible/lab/get_optics_playbook.yml -k -u testuser -vvv
ansible-playbook 2.4.1.0
config file = /home/testuser/ansible.cfg
configured module search path = [u'/home/testuser/.ansible/plugins/modules', 
u'/usr/share/ansible/plugins/modules']
ansible python module location = 
/home/testuser/networking/networking_env/lib/python2.7/site-packages/ansible
executable location = /home/testuser/networking/networking_env/bin/ansible-
playbook
python version = 2.7.12 (default, Dec 15 2016, 15:40:12) [GCC 4.4.7 20120313 
(Red Hat 4.4.7-3)]
Using /home/testuser/ansible.cfg as config file
SSH password: 
Parsed /home/testuser/ansible/hosts/hosts inventory source with ini plugin

PLAYBOOK: get_optics_playbook.yml 

*************************************************************
1 plays in ansible/lab/get_optics_playbook.yml

PLAY [Get 6860 interface levels] 
***************************************************************
META: ran handlers

TASK [Get 6860 Light levels] 
*******************************************************************
task path: /home/afreitag/ansible/lab/get_optics_playbook.yml:8
<10.20.46.4> ESTABLISH CONNECTION FOR USER: testuser on PORT 22 TO 
10.20.46.4
<10.20.46.4> EXEC show interfaces ddm
changed: [10.20.46.4] => {
"changed": true, 
"failed": false, 
"rc": 0, 
"stderr": "", 
"stdout": "  Chas/                                                                      
\r\n  Slot/     Thres-    Temp       Voltage      Tx Bias      Output       
Input\r\n  Port      hold       (C)         (V)          (mA)        (dBm)       
(dBm)\r\n----------+--------+----------+------------+------------+----------
--+------------\r\n 1/1/1     Actual    33.0        3.270        5.100       
-6.192       -4.955    \r\n           A-High    80.0        3.600       
15.000        0.000 

                                <TRUNCATED>

TASK [Append to file] 
**************************************************************************
task path: /home/testuser/ansible/lab/get_optics_playbook.yml:13
Using module file 
/home/testuser/networking/networking_env/lib/python2.7/site-
packages/ansible/modules/files/blockinfile.py
<10.20.46.4> ESTABLISH CONNECTION FOR USER: testuser on PORT 22 TO 
10.20.46.4
<10.20.46.4> EXEC /bin/sh -c 'echo ~ && sleep 0'
<10.20.46.4> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo 
/flash/.ansible/tmp/ansible-tmp-1513920017.26-203252279401130 `" && echo 
ansible-tmp-1513920017.26-203252279401130="` echo 
/flash/.ansible/tmp/ansible-tmp-1513920017.26-203252279401130 `" ) && sleep 
0'
<10.204.46.4> PUT /tmp/tmp6d0OF2 TO /flash/.ansible/tmp/ansible-tmp-
1513920017.26-203252279401130/blockinfile.py
<10.204.46.4> EXEC /bin/sh -c 'chmod u+x /flash/.ansible/tmp/ansible-tmp-
1513920017.26-203252279401130/ /flash/.ansible/tmp/ansible-tmp-
1513920017.26-203252279401130/blockinfile.py && sleep 0'
<10.20.46.4> EXEC /bin/sh -c 
'/home/testuser/networking/networking_env/bin/python 
/flash/.ansible/tmp/ansible-tmp-1513920017.26-
203252279401130/blockinfile.py; rm -rf "/flash/.ansible/tmp/ansible-tmp-
1513920017.26-203252279401130/" > /dev/null 2>&1 && sleep 0'
fatal: [10.20.46.4]: FAILED! => {
"changed": false, 
"failed": true, 
"module_stderr": "", 
"module_stdout": "/bin/sh: 
/home/testuser/networking/networking_env/bin/python: No such file or 
directory\r\n", 
"msg": "MODULE FAILURE", 
"rc": 0
}
    to retry, use: --limit 
@/home/afreitag/ansible/lab/get_optics_playbook.retry

PLAY RECAP 

*******************************************************************

10.20.46.4                : ok=1    changed=1    unreachable=0    failed=1  
python ansible
1个回答
0
投票

由于您的问题中未指定的原因(可能是ansible.cfg中的设置),您尝试使用以下绝对路径在路由器上执行Python可执行文件:/home/testuser/networking/networking_env/bin/python

Ansible报告:“没有这样的文件或目录”,这可能是真的,因为它是一个路由器。


您需要在控制计算机上运行blockinfile作为本地操作。

最简单的可能是为此特定任务设置本地连接:

- name: Append to file
  connection: local
  blockinfile:
    create: yes
    path: '/home/testuser/ansible/output/{{ inventory_hostname }}.txt' 
    block: |
      '{{ command_output.stdout }}'

你也可以使用local_action

- name: Append to file
  local_action:
    module: blockinfile
    create: yes
    path: '/home/testuser/ansible/output/{{ inventory_hostname }}.txt' 
    block: |
      '{{ command_output.stdout }}'

delegate_to声明:

- name: Append to file
  delegate_to: localhost
  blockinfile:
    create: yes
    path: '/home/testuser/ansible/output/{{ inventory_hostname }}.txt' 
    block: |
      '{{ command_output.stdout }}'
© www.soinside.com 2019 - 2024. All rights reserved.