如何在ansible中创建自定义模块

时间:2017-08-27 07:09:37

标签: module ansible

这是我编写的自定义模块,用于从当前系统获取日期时间。我已将模块放在/ usr / share / my_modules文件夹中。

#!/usr/bin/python

import datetime
import json

date = str(datetime.datetime.now())
print(json.dumps({
    "time" : date
}))
def main():
    module = AnsibleModule(
        argument_spec = dict(
            state     = dict(default='present', choices=['present', 'absent']),
            name      = dict(required=True),
            enabled   = dict(required=True, type='bool'),
            something = dict(aliases=['whatever'])
        )
    )
module.exit_json(changed=True, something_else=12345)
module.fail_json(msg="Something fatal happened")
from ansible.module_utils.basic import *
from ansible.module_utils.basic import AnsibleModule
if __name__ == '__main__':
    main()

现在当我尝试使用命令 ansible local -m timetest 执行它时 我收到此错误

127.0.0.1 | FAILED! => {
    "failed": true,
    "msg": "The module timetest was not found in configured module paths. Additionally, core modules are missing. If this is a checkout, run 'git submodule update --init --recursive' to correct this problem."
}

为什么它没有执行我的自定义模块?请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

您可以在Playbook所在的目录中创建library目录,文件结构如下所示:

.
|-- playbook.yml
|-- library
    `-- your-custom-module.py

希望可以帮到你

答案 1 :(得分:0)

您是否尝试过http://ansible-docs.readthedocs.io/zh/stable-2.0/rst/developing_modules.html#testing-modules上的Ansible测试模块说明?

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.File;

public class App 
{
    public static void main( String[] args )
    {
        File sourceZipFile = new File("webjob.zip");
        String kuduApiUrl = "https://yoursitename.scm.azurewebsites.net/api/zip/site/wwwroot/app_data/jobs/triggered/job988/";

        HttpEntity httpEntity = EntityBuilder.create()
        .setFile(sourceZipFile)
        .build();

        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
            "$yoursitename", "SiteLevelPasSw0rD"
        );
        provider.setCredentials(AuthScope.ANY, credentials);

        HttpClient client = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(provider)
            .build();

        HttpPut putRequest = new HttpPut(kuduApiUrl);
        putRequest.setEntity(httpEntity);
        // Kudu's Zip API expects application/zip
        putRequest.setHeader("Content-type", "application/zip");

        try {
            HttpResponse response = client.execute(putRequest);
            int statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            String resBody = EntityUtils.toString(entity, "UTF-8");
            System.out.println(statusCode);
            System.out.println(resBody);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}