2 votes

Comment renommer un fichier avec ansible

J'ai besoin de renommer hosts_example ou hosts_Example en hosts_real si l'un de ces fichiers existe.

- name: Playbook to Standardize  Hosts
  hosts: test
  vars:
    destpath: /etc/hosts_real
    filename: [ /etc/hosts_example,/etc/hosts_Example ]

  tasks:
    - name: Check if file exists
      stat:
        path: "{{ item }}"
      with_items:
        - "{{ filename }}"
      register: check_file_name

    - debug:
        msg: "{{check_file_name}}"

    - name: Rename file
      command: mv "{{ item }}"{{destpath}}"
      with_items:
        - "{{ check_file_name.results }}"
      when: item.stat.exists == true

J'ai essayé, mais j'obtiens des erreurs et je ne parviens pas à obtenir le résultat souhaité.

Voici l'erreur

changed': False, 'stat': {'exists': True, 'path': '/etc/hosts_Example', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 0, 'gid': 0, 'size': 6, 'inode': 112462077, 'dev': 2053, 'nlink': 1, 'atime': 1599813413.9216056, 'mtime': 1599804909.77824, 'ctime': 1599813399.5502446, 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False, 'blocks': 8, 'block_size': 4096, 'device_type': 0, 'readable': True, 'writeable': True, 'executable': False, 'pw_name': 'root', 'gr_name': 'root', 'checksum': '3c31c404fce438e54272bd5c7e67abdcc25e4f22', 'mimetype': 'text/plain', 'charset': 'us-ascii', 'version': '1149597246', 'attributes': ['extents'], 'attr_flags': 'e'}, 'invocation': {'module_args': {'path': '/etc/hosts_Example', 'follow': False, 'get_checksum': True, 'get_mime': True, 'get_attributes': True, 'checksum_algorithm': 'sha1', 'get_md5': None}}, 'failed': False, 'item': '/etc/hosts_Example', 'ansible_loop_var': 'item'}) => {"ansible_loop_var": "item", "changed": true, "cmd": ["mv", "{'changed': False, 'stat': {'exists': True, 'path': '/etc/hosts_Example, 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 0, 'gid': 0, 'size': 6, 'inode': 112462077, 'dev': 2053, 'nlink': 1, 'atime': 1599813413.9216056, 'mtime': 1599804909.77824, 'ctime': 1599813399.5502446, 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False, 'blocks': 8, 'block_size': 4096, 'device_type': 0, 'readable': True, 'writeable': True, 'executable': False, 'pw_name': 'root', 'gr_name': 'root', 'checksum': '3c31c404fce438e54272bd5c7e67abdcc25e4f22', 'mimetype': 'text/plain', 'charset': 'us-ascii', 'version': '1149597246', 'attributes': ['extents'], 'attr_flags': 'e'}, 'invocation': {'module_args': {'path': '/etc/hosts_Example', 'follow': False, 'get_checksum': True, 'get_mime': True, 'get_attributes': True, 'checksum_algorithm': 'sha1', 'get_md5': None}}, 'failed': False, 'item': '/etc/hosts_Example', 'ansible_loop_var': 'item'} /etc/hosts_real"], "delta": "0:00:00.006503", "end": "2020-09-11 09:55:27.442918", "item": {"ansible_loop_var": "item", "changed": false, "failed": false, "invocation": {"module_args": {"checksum_algorithm": "sha1", "follow": false, "get_attributes": true, "get_checksum": true, "get_md5": null, "get_mime": true, "path": "/etc/hosts_Example"}}, "item": "/etc/hosts_Example", "stat": {"atime": 1599813413.9216056, "attr_flags": "e", "attributes": ["extents"], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "3c31c404fce438e54272bd5c7e67abdcc25e4f22", "ctime": 1599813399.5502446, "dev": 2053, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 112462077, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/plain", "mode": "0644", "mtime": 1599804909.77824, "nlink": 1, "path": "/etc/hosts_Example", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 6, "uid": 0, "version": "1149597246", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}}, "msg": "non-zero return code", "rc": 1, "start": "2020-09-11 09:55:27.436415", "stderr": "mv: missing destination file operand after `{\\'changed\\': False, \\'stat\\': {\\'exists\\': True, \\'path\\': \\'/etc/hosts_Example\\', \\'mode\\': \\'0644\\', \\'isdir\\': False, \\'ischr\\': False, \\'isblk\\': False, \\'isreg\\': True, \\'isfifo\\': False, \\'islnk\\': False, \\'issock\\': False, \\'uid\\': 0, \\'gid\\': 0, \\'size\\': 6, \\'inode\\': 112462077, \\'dev\\': 2053, \\'nlink\\': 1, \\'atime\\': 1599813413.9216056, \\'mtime\\': 1599804909.77824, \\'ctime\\': 1599813399.5502446, \\'wusr\\': True, \\'rusr\\': True, \\'xusr\\': False, \\'wgrp\\': False, \\'rgrp\\': True, \\'xgrp\\': False, \\'woth\\': False, \\'roth\\': True, \\'xoth\\': False, \\'isuid\\': False, \\'isgid\\': False, \\'blocks\\': 8, \\'block_size\\': 4096, \\'device_type\\': 0, \\'readable\\': True, \\'writeable\\': True, \\'executable\\': False, \\'pw_name\\': \\'root\\', \\'gr_name\\': \\'root\\', \\'checksum\\': \\'3c31c404fce438e54272bd5c7e67abdcc25e4f22\\', \\'mimetype\\': \\'text/plain\\', \\'charset\\': \\'us-ascii\\', \\'version\\': \\'1149597246\\', \\'attributes\\': [\\'extents\\'], \\'attr_flags\\': \\'e\\'}, \\'invocation\\': {\\'module_args\\': {\\'path\\': \\'/etc/hosts_Example\\', \\'follow\\': False, \\'get_checksum\\': True, \\'get_mime\\': True, \\'get_attributes\\': True, \\'checksum_algorithm\\': \\'sha1\\', \\'get_md5\\': None}}, \\'failed\\': False, \\'item\\': \\'/etc/hosts_Example\\', \\'ansible_loop_var\\': \\'item\\'} /etc/hosts_real'\nTry `mv --help' for more information.", "stderr_lines": ["mv: missing destination file operand after `{\\'changed\\': False, \\'stat\\': {\\'exists\\': True, \\'path\\': \\'/etc/hosts_Example\\', \\'mode\\': \\'0644\\', \\'isdir\\': False, \\'ischr\\': False, \\'isblk\\': False, \\'isreg\\': True, \\'isfifo\\': False, \\'islnk\\': False, \\'issock\\': False, \\'uid\\': 0, \\'gid\\': 0, \\'size\\': 6, \\'inode\\': 112462077, \\'dev\\': 2053, \\'nlink\\': 1, \\'atime\\': 1599813413.9216056, \\'mtime\\': 1599804909.77824, \\'ctime\\': 1599813399.5502446, \\'wusr\\': True, \\'rusr\\': True, \\'xusr\\': False, \\'wgrp\\': False, \\'rgrp\\': True, \\'xgrp\\': False, \\'woth\\': False, \\'roth\\': True, \\'xoth\\': False, \\'isuid\\': False, \\'isgid\\': False, \\'blocks\\': 8, \\'block_size\\': 4096, \\'device_type\\': 0, \\'readable\\': True, \\'writeable\\': True, \\'executable\\': False, \\'pw_name\\': \\'root\\', \\'gr_name\\': \\'root\\', \\'checksum\\': \\'3c31c404fce438e54272bd5c7e67abdcc25e4f22\\', \\'mimetype\\': \\'text/plain\\', \\'charset\\': \\'us-ascii\\', \\'version\\': \\'1149597246\\', \\'attributes\\': [\\'extents\\'], \\'attr_flags\\': \\'e\\'}, \\'invocation\\': {\\'module_args\\': {\\'path\\': \\'/etc/hosts_Example\\', \\'follow\\': False, \\'get_checksum\\': True, \\'get_mime\\': True, \\'get_attributes\\': True, \\'checksum_algorithm\\': \\'sha1\\', \\'get_md5\\': None}}, \\'failed\\': False, \\'item\\': \\'/etc/hosts_Example\\', \\'ansible_loop_var\\': \\'item\\'} /etc/hosts_real'", "Try `mv --help' for more information."], "stdout": "", "stdout_lines": []}

Editer 1 :

J'ai modifié la tâche

- name: Rename file
  shell: " mv {{ item }} {{destpath}}  "
  with_items:
    - "{{ check_file_name.results }}"
  when: item.stat.exists == true

Je vois maintenant l'erreur suivante

["mv: target `/etc/hosts_real' is not a directory"]

3voto

Gerald Schneider Points 17588

Vous donnez trop de paramètres à mv . item contient de nombreuses informations, vous n'avez besoin que du chemin d'accès. Utiliser {{ item.stat.path }} au lieu de cela.

Ceci devrait suffire :

- name: Rename file
  command: mv "{{ item.stat.path }}" "{{destpath}}"
  with_items:
    - "{{ check_file_name.results }}"
  when: item.stat.exists == true

Vous avez également eu un mising " dans cette ligne.

Il convient également de noter que with_items est obsolète, vous devriez transformer en boucle .

SistemesEz.com

SystemesEZ est une communauté de sysadmins où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres sysadmins, poser vos propres questions ou résoudre celles des autres.

Powered by:

X