Giter Club home page Giter Club logo

go-ansible's People

Contributors

abergmeier avatar alexandrevilain avatar apenella avatar asafalima avatar chungeun-choi avatar dependabot[bot] avatar edo-aleix-penella avatar eugenebad avatar febrianrendak avatar gciavarrini avatar jgengo avatar lovexayah avatar lucabodd avatar rvben avatar sestegra avatar svasilevski avatar zhangguanzhang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-ansible's Issues

Add virtualenv option

Hello,

Right now it's only possible to launch with the default ansible-playbook binary.

As you may know managing Ansible modules directly through default pip installed on the system is a nightmare (I have already broken machine myself ;). The best practice we have is to always set it up through virtualenv and I think it's pretty conventional.

I am aware that it would work when the virtualenv is already loaded, but it would be nice to provide a way to overwrite the default binary. Similar to how ansible provide the ansible_python_interpreter var

I can submit a PR

Typo "resutls"

Both in `docs/upgrade_guide_to_1.0.0.md` and in `pkg/stdoutcallback/results/JSONResults.go` there is the typo "resutls".

Async callback

Right now the only way to have result while launching a playbook is to launch ansible-playbook with de default callback. But this doesn't allow the use of result on the fly.

All other callback are synchronous, the execution need finish to get result (ex: json callback)

I went through the Ansible community callback plugins but none would enable async. Though it was been done for specific use case like AWX or ARA.

Would you be open to the idea of adding such feature ? It would require writing a custom Ansible callback plugin

Support multiple playbooks using results.JSONParse

When executing multiple playbooks in playbook.AnsiblePlaybookCmd, the JSON parsing doesn't work.

For instance, I tested with examples/json-stdout-ansibleplaybook/json-stdout-ansibleplaybook.go.

--- a/examples/json-stdout-ansibleplaybook/json-stdout-ansibleplaybook.go
+++ b/examples/json-stdout-ansibleplaybook/json-stdout-ansibleplaybook.go
@@ -33,7 +33,7 @@ func main() {
        )
 
        playbook := &playbook.AnsiblePlaybookCmd{
-               Playbooks:         []string{"site.yml"},
+               Playbooks:         []string{"site.yml", "site.yml"},
                Exec:              execute,
                ConnectionOptions: ansiblePlaybookConnectionOptions,
                Options:           ansiblePlaybookOptions,

The result is following.

panic: Unmarshall error
        invalid character '{' after top-level value

goroutine 1 [running]:
main.main()
        /workspace/go-ansible/examples/json-stdout-ansibleplaybook/json-stdout-ansibleplaybook.go:50 +0x2bd
exit status 2

playbook.AnsiblePlaybookCmd uses Playbooks fields as an array of playbook's name, so I suggest to have an array of AnsiblePlaybookJSONResults as results.JSONParse output.

feature: support interactive input from stdin

I have a playbook file which requires user input in order to progress. Currently, when invoking an interactive playbook from go-ansible default values for user input are taken.

How do you think this might be supported ? I was thinking of piping the stdin of go-ansible to ansible's stdin

Ansible command

Hi

I am looking to run the following command . Just wondering if its possible using the go-ansible , Any pointers how I can run following command

ansible-playbook -i hosts.yml platform.all

help with remote options

how can i connect to remote host to run ansible-playbook,the options in the code are local,can somebody share a remote config?

results.JSONParse error

Multiple types of plays.tasks.hosts.msg

panic: Unmarshall error json: cannot unmarshal array into Go struct field AnsiblePlaybookJSONResultsPlayTaskHostsItem.plays.tasks.hosts.msg of type string

{
    "custom_stats": {},
    "global_custom_stats": {},
    "plays": [
        {
            "play": {
                "duration": {
                    "end": "2021-08-03T03:48:07.438294Z",
                    "start": "2021-08-03T03:48:06.634238Z"
                },
                "id": "000c29ae-db4d-69ff-80c5-000000000009",
                "name": "Welcome"
            },
            "tasks": [
                { 
                    "hosts": { 
                        "localhost": {
                            "_ansible_no_log": false,
                            "_ansible_verbose_always": true,
                            "action": "debug",
                            "changed": false,
                            "msg": [
                                "*****************************************************************",
                                " ",
                                "*****************************************************************"
                            ]  
                        }
                    }
}

Add Ansible ad-hoc

Hi,

Would it make sense to add the ad hoc module in this repo?

I really like how this repo enable to use playbook with go and I would like to use the mechanism for ad-hoc command. I'm wondering if I should create a new repo or if is something we could add here.

How to pass ansible environment variables for the ansible playbook?

Hi @apenella, I am trying to understand how I can set the env variables for running Ansible playbook. I tried setting the env variable "ANSIBLE_LOG_PATH" by doing os.Setenv("ANSIBLE_LOG_PATH", "test.log") before triggering the following command. The ansible does not seem to log even with the env set. Could you please help me with this?

The code snippet is as follows

        os.Setenv("DEFAULT_LOG_PATH", "test.log")  
	ansibleConnectionOption := &playbookOptions.AnsibleConnectionOptions{
		Connection: "local",
	}

	ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
		ExtraVars: extraVars,
	}

	playbook := &playbook.AnsiblePlaybookCmd{
		Playbooks:         []string{playFile},
		ConnectionOptions: ansibleConnectionOption,
		Options:           ansiblePlaybookOptions,
	}

	return playbook.Run(context.Background())```

How to kill the process

DefaultExecute Execute function don't save exec.Command object, I can't call cmd.Process.Kill() to kill the ansible-playbook process.

How to get ansible log as JSON?

I've read doc but i did not understand, how to get ansible log as JSON?

	playbook := &ansibler.AnsiblePlaybookCmd{
		Playbook: fmt.Sprintf(playbookPath, "test"),
		Options:  ansiblePlaybookOptions,
		Writer:   &AnsibleLog{},
		StdoutCallback: "json",
	}

code below make a panic

panic: send on closed channel

goroutine 7 [running]:
github.com/apenella/go-ansible/execute.(*DefaultExecute).Execute.func1(0xc0001c2080, 0x0, 0x0, 0x16e5b00, 0xc0000106b0, 0xc000028840, 0xc0000287e0)
        /Users/dikkini/go/pkg/mod/github.com/apenella/[email protected]/execute/defaultExecute.go:92 +0x158
created by github.com/apenella/go-ansible/execute.(*DefaultExecute).Execute
        /Users/dikkini/go/pkg/mod/github.com/apenella/[email protected]/execute/defaultExecute.go:82 +0x508
Exiting.

Also, there is a misstake in example https://github.com/apenella/go-ansible/blob/master/examples/simple-ansibleplaybook-json/simple-ansibleplaybook-json.go#L41

Error: Cannot use 'buff.Bytes()' (type []byte) as type *bufio.Reader

Is their a mechanism for hostvars?

Im building my inventory dynamically, but I want to use host variables, ie assume the following traditional inventory:

Can this library implement host variables that would have the same effect as this inventory file?

all:
  vars:
    new_kernel_version: kernel-3.10.0-1160.36.2.el7.x86_64
  children:
    cluster:
      hosts:
        host1:
          ansible_host: host1.example.com
          rack: rack1
          platform: xyz

Can you construct a playbook fully with code?

Right now my playbooks are local yaml files. I was curious if you can completely construct a playbook in code with this library and completely removed the dependence on static files?

How to define variable for inventory and extra-vars.??

I am using go func will which pass the hostname and dbname to the ansiblePlaybookOptions but i am trying to use that variable getting below error.

 ansiblePlaybookOptions := &ansibler.AnsiblePlaybookOptions{
		Inventory: "%s",host
		ExtraVars: map[string]interface{}{
			"dbname":    "%s",dbname

		},
	}

syntax error: unexpected newline, expecting comma or }

ssh port change

I have modified the port of Ansible SSH and now use Go-Ansible to run Ansible PlayBook. How can I run it properly。

AnsibleAdhocCmd return error when args has spaces

When I use AnsibleAdhocCmd w/ Args('ping x.x.x.x -c 10'), I see this error. This command works in shell. The root cause is there are spaces in teh args. Any solution?

*errors.Error(&errors.Error{context:"(DefaultExecute::Execute)", message:"Error during command execution: ansible-playbook error: one or more host failed\n\nCommand executed: /usr/bin/ansible all --args 'ping x.x.x.x -c 10' --inventory x.x.x.x, --module-name command --private-key ~/.ssh/gpc_rack_id_rsa\n\nexit status 2", wrappedErrors:[]error(nil)})

Execute ansible-galaxy commands

Enhacement request
Execute ansible-galaxy commands using go-ansible package.

  • Main command options:
usage: ansible-galaxy [-h] [--version] [-v] TYPE ...

Perform various Role and Collection related operations.

positional arguments:
  TYPE
    collection   Manage an Ansible Galaxy collection.
    role         Manage an Ansible Galaxy role.

optional arguments:
  --version      show program's version number, config file location, configured module search path, module location, executable location and exit
  -h, --help     show this help message and exit
  -v, --verbose  verbose mode (-vvv for more, -vvvv to enable connection debugging)
  • Collection subcommand options:
usage: ansible-galaxy collection [-h] COLLECTION_ACTION ...

positional arguments:
  COLLECTION_ACTION
    download         Download collections and their dependencies as a tarball for an offline install.
    init             Initialize new collection with the base structure of a collection.
    build            Build an Ansible collection artifact that can be publish to Ansible Galaxy.
    publish          Publish a collection artifact to Ansible Galaxy.
    install          Install collection(s) from file(s), URL(s) or Ansible Galaxy
    list             Show the name and version of each collection installed in the collections_path.
    verify           Compare checksums with the collection(s) found on the server and the installed copy. This does not verify dependencies.

optional arguments:
  -h, --help         show this help message and exit
  • Role subcommand options:
usage: ansible-galaxy role [-h] ROLE_ACTION ...

positional arguments:
  ROLE_ACTION
    init       Initialize new role with the base structure of a role.
    remove     Delete roles from roles_path.
    delete     Removes the role from Galaxy. It does not remove or alter the actual GitHub repository.
    list       Show the name and version of each role installed in the roles_path.
    search     Search the Galaxy database by tags, platforms, author and multiple keywords.
    import     Import a role into a galaxy server
    setup      Manage the integration between Galaxy and the given source.
    info       View more details about a specific role.
    install    Install role(s) from file(s), URL(s) or Ansible Galaxy

optional arguments:
  -h, --help   show this help message and exit

Ansible playbook with wrong IP hang up

I launch ansible playbook using go-ansible with wrong IP address (real IP is 192.168.2.10, i specify 192.168.2.12 - no host with such IP address). It hangup for more than 600 seconds. Sometimes 1 hour timeout, before i get an error that host is unreachable. No specific settings in code, just run playbook with specific inventory. If i sepcify wrong ssh port (23 instead of 22) - i will get an error in 10-15 seconds.

If i run ansible playbook command directly - i'll get an error very fast.

If it is possible to add a list variable as extra-vars

ansible-playbook arcade.yml --extra-vars '{"pacman":"mrs","ghosts":["inky","pinky","clyde","sue"]}'

in the example of go-ansible:

ansiblePlaybookOptions.AddExtraVar("app_name", app_name)

The value of the extra-vars must be string?
If it is possible to add a list variable as extra-vars?
Thank you very much

How to connection other nodes?

ansiblePlaybookConnectionOptions := &ansibler.AnsiblePlaybookConnectionOptions{
Connection: "local",
}

when i chnage Connection: local to Connection:<my node ip> 。

ansiblePlaybookConnectionOptions := &ansibler.AnsiblePlaybookConnectionOptions{
Connection: "192.168.1.176",
}

Additional context:

Go-ansible example => fatal: [192.168.1.176]: FAILED! => {"msg": "the connection plugin '192.168.1.176' was not found"}
Go-ansible example => to retry, use: --limit @/root/go/src/github.com/apenella/go-ansible/examples/simple-ansibleplaybook/site.retry
Go-ansible example =>
Go-ansible example => PLAY RECAP

Typos in README.md

There are a number of typos in the README file.
lastest > latest
recieves > receives
ansbile > ansible

Duration + Json parsing

if i make:

execute := execute.NewDefaultExecute(
	execute.WithWrite(io.Writer(buff)),
	execute.WithShowDuration(),
)

then:

StdoutCallback:    	"json",
Exec: 			execute,

then:

err := playbook.Run(context.TODO())
res, err = results.JSONParse(buff.Bytes())

it will be error:

panic: Unmarshall error
	invalid character 'D' after top-level value

goroutine 1 [running]:

Because of line in the end of output: "Duration: 5.348349791s"

Sorry, if it's my mistake! Amazing rep!

warning messages are included on json data when is used json stdout_callback

Which ansible version are you using?
ansible 2.10.5
config file = ??/.ansible.cfg
configured module search path = ['??/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = ??/.local/lib/python3.8/site-packages/ansible
executable location = ??/.local/bin/ansible
python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]

Which go version are you using?
go version go1.15.6 linux/amd64

Which go-ansbile version are you using?
0.7.0

What did you see?
When is run a playbook using the json stdout_callbak, and there is a warning message on the output (with warning messages I mean those messages that start with [WARNING]), that message is included on json data and then the parsing done by JSONParse fails.

exit status 4                                                                                  
panic: Unmarshall error                     
        invalid character '[' after top-level value

On the other hand, some warning messages returned by ansible-playbook in one line on terminal are seen as multiple line messages by go-ansible

Thats an example:

  • Shell output:
$ ANSIBLE_STDOUT_CALLBACK=json ansible-playbook  --inventory 127.0.0.1,  --user apenella site.yml
[WARNING]: Unhandled error in Python interpreter discovery for host 127.0.0.1: Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 22: Connection refused
...
  • go-ansible output:
-- #[WARNING]: Unhandled error in Python interpreter discovery for host 127.0.0.1:#
-- #Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 22:#
-- #Connection refused#

How to reproduce the issue?
To reproduce the issue I used simple-ansibleplaybook-json example.
I forced a connection error updating ansiblePlaybookConnectionOptions definition as is shown below:

	ansiblePlaybookConnectionOptions := &ansibler.AnsiblePlaybookConnectionOptions{
	//	Connection: "local",
		User:       "apenella",
	}

You could see the scanned text by go-ansible updating ansiblePlaybookJSONResults as is shown below:

	for scanner.Scan() {
		line := scanner.Text()
		if !skipLine(line) {
                        // println for debuggin purpose
                        fmt.Println("-- #" + line + "#")
			fmt.Fprintf(w, "%s", line)
		}
	}

The issue is related to #23

Execute ansible-vault command

Enhancement request
Execute ansible-vault command using go-ansible.
edit subcommand could be excluded.

usage: ansible-vault [-h] [--version] [-v] {create,decrypt,edit,view,encrypt,encrypt_string,rekey} ...

encryption/decryption utility for Ansible data files

positional arguments:
  {create,decrypt,edit,view,encrypt,encrypt_string,rekey}
    create              Create new vault encrypted file
    decrypt             Decrypt vault encrypted file
    edit                Edit vault encrypted file
    view                View vault encrypted file
    encrypt             Encrypt YAML file
    encrypt_string      Encrypt a string
    rekey               Re-key a vault encrypted file

optional arguments:
  --version             show program's version number, config file location, configured module search path, module location, executable location and exit
  -h, --help            show this help message and exit
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable connection debugging)

Help with StdoutCallbacks

Im writing a large automation that will be running several playbooks and many adhoc commands. Is there a way to get stdout/stderr from ansible commands using the shell/command modules in json or some structured output that can be easily parse?

I can only seem to get output like this:

msg="running ansible command: ansible all  --args uname -r --extra-vars '{"ansible_sudo_pass":"mysecret"}' --forks 5 --inventory myhost,  --timeout 30  --become-method sudo --become-user root"
msg="── myhost | CHANGED | rc=0 >>"
msg="── 3.10.0-1160.31.1.el7.x86_64"
── myhost | CHANGED | rc=0 >>
── 3.10.0-1160.31.1.el7.x86_64

My transformer:

func parseKernelVersion() results.TransformerFunc {
    return func(s string) string {
        log.Debugf("%s", s)
        return s
    }
}

Then how I set it up:

    command.Exec = execute.NewDefaultExecute(execute.WithTransformers(parseKernelVersion()))
    // I've tried setting this to "" and few other options
    command.StdoutCallback = "oneline"
    //command.StdoutCallback = "json"

Is there a way to get stdout/stderr back in a json structure of some sort here?

about ansible.cfg

while i worked on this project,i find it doens't run ansible-playbook as i think,i change ansible.cfg log_path,but the go-ansible's log doesn't record ansible's output there.
i read the code but don't find any config about log,does the execute.WithWrite work?
here's code
file,err:= os.OpenFile("xayah.log",os.O_RDWR | os.O_CREATE | os.O_APPEND,0766)

playbook := &playbook.AnsiblePlaybookCmd{
	Playbooks:         []string{"xayah.yml"},
	ConnectionOptions: ansiblePlaybookConnectionOptions,
	Options:           ansiblePlaybookOptions,
	Exec: execute.NewDefaultExecute(
		execute.WithTransformers(
			results.Prepend("Go-ansible example"),
		),
		execute.WithWrite(file),
	),
	StdoutCallback: "json",
}

just find that does't work.

exec: "ansible-playbook": executable file not found in %PATH%

windows vscode use

panic: Binary file 'ansible-playbook' does not exists
        exec: "ansible-playbook": executable file not found in %PATH%

goroutine 1 [running]:

linux all normal

windows application how to set this individual PATH 。ansible-playbook file create ok

thanks

adhoc command StdoutCallback as json raise err.

I have some code as following:


func CheckAssetsConnectivityManual(ctx context.Context, adminUser, password, hosts string) {

	buff := new(bytes.Buffer)

	ansibleConnectionOptions := &options.AnsibleConnectionOptions{
		User: adminUser,
	}

	ansibleAdhocOptions := &adhoc.AnsibleAdhocOptions{
		Inventory:  hosts,
		ModuleName: "ping",
		//ModuleName: "setup",
	}

	// install sshpass
	ansibleAdhocOptions.AddExtraVar("ansible_password", password)

	executor := execute.NewDefaultExecute(
		execute.WithWrite(io.Writer(buff)),
	)

	adhoc := &adhoc.AnsibleAdhocCmd{
		Pattern:           "all",
		Options:           ansibleAdhocOptions,
		ConnectionOptions: ansibleConnectionOptions,
		Exec:              executor,
		StdoutCallback:    "json",
	}

	err := adhoc.Run(ctx)
	if err != nil {
		log.Println(err.Error())
	}
	log.Println(" ------ ===== buff: ", string(buff.Bytes()))
	res, err := results.JSONParse(buff.Bytes())
	if err != nil {
		log.Println(err.Error())
	}
	log.Println(" ---- res : ", res.String())

}


res, err := results.JSONParse(buff.Bytes())

panic.

ExtraVars not enclosed in quotes

The ansible command that is being generated when adding extra vars argument is

ansible-playbook --extra-vars {"EC2IP":"127.0.0.1","ansible_password":"123456"} --inventory 10.10.10.120, --user root change-settings.yml

This is not working as extra-vars json should be enclosed in qoutes like

ansible-playbook --extra-vars '{"EC2IP":"127.0.0.1","ansible_password":"123456"}' --inventory 10.10.10.120, --user root change-settings.yml

Problem with unreachable host

Hi @apenella
When I excute with command----(1.1.1.1 is unreachable host for test)
/opt/homebrew/bin/ansible-playbook --extra-vars '{"host_name":"10.200.75.152,10.200.75.136,1.1.1.1"}' --inventory 10.200.75.136,10.200.75.152,1.1.1.1 --user root nstall-nginx.yml
is ok.
but with go-ansible i got a panic:
panic: Error during command execution: ansible-playbook error: parser error
tips:when i del 1.1.1.1, it just ok with go-ansible

code:

func ansiblePlaybookTask(_, f, inventory string, jsonvars map[string]interface{}){
ansiblePlaybookConnectionOptions := &options.AnsibleConnectionOptions{
//Connection: "local",
User: "root",
}

ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Inventory: inventory,
//Inventory: "/root/inventory",
ExtraVars: jsonvars,
}

playbook := &playbook.AnsiblePlaybookCmd{
Playbooks: []string{f},
ConnectionOptions: ansiblePlaybookConnectionOptions,
Options: ansiblePlaybookOptions,
StdoutCallback: "json",
}

err := playbook.Run(context.TODO())
if err != nil {
panic(err)
}
}

thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.