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

Ansible stat模块不工作
EN

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

我需要一些帮助:

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

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

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

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

roles/ansible/tasks/main.yml

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
---
- 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
代码运行次数:0
运行
AI代码解释
复制
---
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
代码运行次数:0
运行
AI代码解释
复制
---
- hosts: controller
  become: yes

  roles:
    - ansible
...

and the output:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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-27 19: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-27 16:06:51

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

调试输出显示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
stat_ansible_config.stat.exists = True
stat_inventory.stat.exists = False

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

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

https://stackoverflow.com/questions/41896970

复制
相关文章
Ansible-stat模块
https://docs.ansible.com/ansible/latest/modules/stat_module.html#stat-module
星哥玩云
2022/09/15
6090
Python stat 模块
fileStats = os.stat ( 'test.txt' )  #获取文件/目录的状态 fileInfo = { 'Size':fileStats [ stat.ST_SIZE ],  #获取文件大小 'LastModified':time.ctime( fileStats [ stat.ST_MTIME ] ),  #获取文件最后修改时间 'LastAccessed':time.ctime( fileStats [ stat.ST_ATIME ] ),  #获取文件最后访问时间 'CreationTime':time.ctime( fileStats [ stat.ST_CTIME ] ),  #获取文件创建时间 'Mode':fileStats [ stat.ST_MODE ]  #获取文件的模式 } #print fileInfo
py3study
2020/01/07
5240
Ansible 获取主机信息模块setup、获取文件详细信息模块stat(学习笔记十)
1、获取setup的所有信息,获取的信息有上百条: ansible all -m setup "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.xx.xx.xxx" ], "ansible_all_ipv6_addresses": [ "fe80::250:56ff:febc:5e1d" ], .......................................... .......................................... "module_setup": true }, "changed": false }
用户5760343
2022/05/24
1.6K0
Ansible 获取主机信息模块setup、获取文件详细信息模块stat(学习笔记十)
Ansible 模块
bash无论在命令行上执行,还是bash脚本中,都需要调用cd、ls、copy、yum等命令;模块就是Ansible的“命令”,模块是ansible命令行和脚本中都需要调用的。常用的Ansible模块有yum、copy、template等。
Alone-林
2023/03/17
1.4K0
Ansible模块介绍
描述:ansible使用ansible-doc --list可以看见所有的模块,ansble-doc -s模块名称显示模块使用详情;
全栈工程师修炼指南
2022/09/28
2.8K0
Ansible模块介绍
Ansible模块介绍
描述:ansible使用ansible-doc --list可以看见所有的模块,ansble-doc -s模块名称显示模块使用详情;
全栈工程师修炼指南
2020/10/26
3K0
Ansible模块介绍
Ansible模块基础使用
ansible通过各种模块完成操作,除了ping模块,其他模块可以使用如下命令查看 ansible -l
陈不成i
2021/08/02
5480
Ansible-hostname模块
https://docs.ansible.com/ansible/latest/modules/hostname_module.html#hostname-module
星哥玩云
2022/09/15
6830
Ansible-script模块
https://docs.ansible.com/ansible/latest/modules/script_module.html#script-module
星哥玩云
2022/09/15
1.1K0
Ansible-group模块
https://docs.ansible.com/ansible/latest/modules/group_module.html#group-module
星哥玩云
2022/09/15
3130
Ansible-fetch模块
https://docs.ansible.com/ansible/latest/modules/fetch_module.html#fetch-module
星哥玩云
2022/09/15
7770
Python ansible常用模块
import ansible.runner import ansible.playbook import ansible.inventory from ansible import callbacks from ansible import utils
py3study
2020/01/10
6680
Ansible常用模块介绍
1、 ansible-doc 希望知道更加详细的module的信息,最好的方法是使用ansible自带的ansible-doc的-s选项 [root@node1 ~]# ansible-doc -s
程裕强
2018/01/02
1.7K0
ansible之synchronize模块
基于ansible2.9 选项 参数 备注 archive yesno 启用递归、链接、权限、时间、所有者、组 compress yesno 在传输过程中压缩文件,在大多数情况下,请启用该功能 delete yesno 删除dest中存在,src中不存在的文件,需要设置recursive=yes dest 必选 目标路径,可以是绝对路径,也可以是相对路径 dest_port 目标端口,目标主机上的ssh端口号 group yesno 保留组 mode pullpush 模式,指定同步的方向,在push模式
陳斯托洛夫斯記
2022/10/27
5740
Ansible-copy模块
https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module
星哥玩云
2022/09/15
8210
Ansible-setup模块
setup模块用于收集远程主机的基本信息(如操作系统类型,主机名,ip,cpu信息,内存信息等)
星哥玩云
2022/09/15
8530
ansible模块定制开发
现以pids.py模块进行讲解,该文件位于ansible/modules/system/pids.py,一旦你理解模块的基本开发流程,就可以开发的模块,让自己的能力更上一层楼:
yxxhero
2022/05/31
1K0
03-Ansible模块
如果命令比较多可以写成一个脚本,然后使用ansible把这个脚本推送到远程主机执行
小朋友呢
2020/01/11
1.1K0
ansible之user模块
管理用户 选项 参数 备注 append yesno 如果为yes,把用户添加到指定的组如果为no,仅将用户添加到指定的组中,并将其从其他组中移除 create_home yesno 是否创建用户家目录 force yesno generate_ssh_key yesno 是否为用户生成ssh密钥,不会覆盖现有的ssh密钥,除非force=yes group 设置用户的组 groups 用户将被添加到的组列表 home 设置用户的家目录 name 必选项 要创建、删除、修改的用户名称 passwor
陳斯托洛夫斯記
2022/10/27
3460
Ansible-service模块
https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module
星哥玩云
2022/09/15
7080

相似问题

fstat工作,stat不工作。

10

Ansible uri模块授权不按预期工作

13

Ansible 2.5.5 - 'replace‘模块中的before属性不工作

160

stat寄存器在ansible

10

Ansible replace模块不能正常工作

11
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文