首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ansible stat模块不工作

Ansible stat模块不工作
EN

Stack Overflow用户
提问于 2017-01-27 23:02:38
回答 2查看 3.2K关注 0票数 1

我需要一些帮助:

我正在使用Vagrant创建虚拟机,以便在它们上配置一些集群。对于我想用作Ansible控制节点的第一个VM,我从本地机器运行Ansible,以便在我的control node中安装和配置Ansible。

当我尝试检查.inventory文件是否存在并尝试将该文件复制到主目录(在虚拟机中)时出现问题。

我对清单文件使用的命令只适用于检查.ansible.cfg文件的状态,而不是清单文件的状态。

你们知道我做错了什么吗?

roles/ansible/tasks/main.yml

代码语言:javascript
运行
复制
---
- name: install epel-release
  yum:
    name: epel-release
    state: present

- name: install ansible
  yum:
    name: ansible
    state: present

- name: stat ansible configuration file
  stat:
    path: "{{ cfg_file }}"
  register: stat_ansible_config

- name: copy .ansible.cfg to home directory
  copy:
    src: .ansible.cfg
    dest: /home/{{ user }}/.ansible.cfg
    owner: "{{ user }}"
    group: "{{ group }}"
    mode: 0644
  when: stat_ansible_config.stat.exists

- name: stat ansible inventory file
  stat:
    path: "{{ inventory_file }}"
  register: stat_inventory

- name: copy .inventory to home directory
  copy:
    src: .inventory
    dest: /home/{{ user }}/.inventory
    owner: "{{ user }}"
    group: "{{ group }}"
    mode: 0644
  when: stat_inventory.stat.exists
...

roles/ansible/vars/main.yml

代码语言:javascript
运行
复制
---
user: vagrant
group: vagrant
cfg_file:       /{{ user }}/provision/playbooks/roles/ansible/files/.ansible.cfg
inventory_file: /{{ user }}/provision/playbooks/roles/ansible/files/.inventory
...

the playbook:

代码语言:javascript
运行
复制
---
- hosts: controller
  become: yes

  roles:
    - ansible
...

and the output:

代码语言:javascript
运行
复制
TASK [ansible : stat ansible configuration file] *******************************

ok: [controller] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "checksum_algorithm": "sha1",
            "follow": false,
            "get_checksum": true,
            "get_md5": true,
            "mime": false,
            "path": "/vagrant/provision/playbooks/roles/ansible/files/.ansible.cfg"
        },
        "module_name": "stat"
    },
    "stat": {
        "atime": 1485527858.0,
        "checksum": "46acc076fda7e38fd7262fbc88f8ab4e1f52ddca",
        "ctime": 1485452789.0,
        "dev": 38,
        "executable": false,
        "exists": true,
        "gid": 1000,
        "gr_name": "vagrant",
        "inode": 118,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "md5": "0cb8c97246776dc7e88fe44f19c3278f",
        "mode": "0644",
        "mtime": 1485452789.0,
        "nlink": 1,
        "path": "/vagrant/provision/playbooks/roles/ansible/files/.ansible.cfg",
        "pw_name": "vagrant",
        "readable": true,
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 164,
        "uid": 1000,
        "wgrp": false,
        "woth": false,
        "writeable": true,
        "wusr": true,
        "xgrp": false,
        "xoth": false,
        "xusr": false
    }
}

TASK [ansible : copy .ansible.cfg to home directory] ***************************

ok: [controller] => {
    "changed": false,
    "checksum": "46acc076fda7e38fd7262fbc88f8ab4e1f52ddca",
    "dest": "/home/vagrant/.ansible.cfg",
    "diff": {
        "after": {
            "path": "/home/vagrant/.ansible.cfg"
        },
        "before": {
            "path": "/home/vagrant/.ansible.cfg"
        }
    },
    "gid": 1000,
    "group": "vagrant",
    "invocation": {
        "module_args": {
            "backup": null,
            "content": null,
            "delimiter": null,
            "dest": "/home/vagrant/.ansible.cfg",
            "diff_peek": null,
            "directory_mode": null,
            "follow": false,
            "force": false,
            "group": "vagrant",
            "mode": 420,
            "original_basename": ".ansible.cfg",
            "owner": "vagrant",
            "path": "/home/vagrant/.ansible.cfg",
            "recurse": false,
            "regexp": null,
            "remote_src": null,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": ".ansible.cfg",
            "state": null,
            "unsafe_writes": null,
            "validate": null
        }
    },
    "mode": "0644",
    "owner": "vagrant",
    "path": "/home/vagrant/.ansible.cfg",
    "secontext": "unconfined_u:object_r:user_home_t:s0",
    "size": 164,
    "state": "file",
    "uid": 1000
}

TASK [ansible : stat ansible inventory file] ***********************************

ok: [controller] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "checksum_algorithm": "sha1",
            "follow": false,
            "get_checksum": true,
            "get_md5": true,
            "mime": false,
            "path": null
        },
        "module_name": "stat"
    },
    "stat": {
        "exists": false
    }
}

TASK [ansible : copy .inventory to home directory] *****************************

task path: /Users/alessandro/Go/src/github.com/alesr/neo4go/provision/playbooks/roles/ansible/tasks/main.yml:31
skipping: [controller] => {
    "changed": false,
    "skip_reason": "Conditional check failed",
    "skipped": true
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-28 03:48:41

inventory_file是一个magic variable,由Ansible在playbook运行期间设置,覆盖您尝试分配的任何值。

在inventory stat任务中,您可能会注意到:invocation.module_args.path: null

将您的inventory_file变量重命名为my_inventory_file,它将起作用。

票数 2
EN

Stack Overflow用户

发布于 2017-01-28 00:06:51

copy发现文件已经存在,并表示没有更改,但与您的源文件相同,不做任何更改。

调试输出显示:

代码语言:javascript
运行
复制
stat_ansible_config.stat.exists = True
stat_inventory.stat.exists = False

这就解释了行为上的差异。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41896970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档