Giter Club home page Giter Club logo

Comments (4)

apenella avatar apenella commented on May 28, 2024

Hi @lingxiao26

I tried to build the example and seems to work as expected, and the files are embed in the binary.
That what I did, can you try it?

go-ansible/examples/ansibleplaybook-simple-embedfs $ go build .
go-ansible/examples/ansibleplaybook-simple-embedfs $ ls
ansibleplaybook-simple-embedfs  ansibleplaybook-simple-embedfs.go  embedfs
go-ansible/examples/ansibleplaybook-simple-embedfs $ mv  ansibleplaybook-simple-embedfs /tmp
go-ansible/examples/ansibleplaybook-simple-embedfs $ cd /tmp
aleix@EDO-LAP-2060: /tmp
❯ ./ansibleplaybook-simple-embedfs 
Copying file from the embedded filesystem 'embedfs/inventory.ini' to '/tmp/ansibleplaybook-simple-embedfs-2176152074/inventory.ini'
Copying file from the embedded filesystem 'embedfs/site.yml' to '/tmp/ansibleplaybook-simple-embedfs-2176152074/site.yml'
Copying file from the embedded filesystem 'embedfs/site2.yml' to '/tmp/ansibleplaybook-simple-embedfs-2176152074/site2.yml'

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]

TASK [ansibleplaybook-simple] **************************************************
ok: [127.0.0.1] => 
  msg: Your are running 'ansibleplaybook-simple-embedfs' example

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


PLAY [all] *********************************************************************

TASK [ansibleplaybook-simple] **************************************************
ok: [127.0.0.1] => 
  msg: Your are running 'ansibleplaybook-simple-embedfs' example on site2.yml

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

from go-ansible.

rhugga avatar rhugga commented on May 28, 2024

One suggestion. What I ended up doing is creating structs that you can yaml-marshal into viable ansible playbooks. It's not perfect and you might run into issues doing this with some of the more complex ansible modules. Then when I go to run a playbook, I serialize it into TMPDIR, run it from there, then remove the playbook. I no longer have playbooks embedded in the code or have long-living playbooks on disk.

For example, here is a playbook that uses ansible copy module:

type CopyPlaybook struct {
	Name           string           `yaml:"name"`
	Hosts          string           `yaml:"hosts"`
	GatherFacts    bool             `yaml:"gather_facts,omitempty"`
	Become         bool             `yaml:"become,omitempty"`
	IgnoreErrors   bool             `yaml:"ignore_errors,omitempty"`
	AnyErrorsFatal bool             `yaml:"any_errors_fatal,omitempty"`
	Tasks          []CopyModuleTask `yaml:"tasks"`
}

// CopyModuleTask encapsulates options for the ansible copy module
type CopyModuleTask struct {
	Name       string         `yaml:"name"`
	Register   string         `yaml:"register,omitempty"`
	FailedWhen []string       `yaml:"failed_when,omitempty"`
	DelegateTo string         `yaml:"delegate_to,omitempty"`
	Module     CopyModuleArgs `yaml:"ansible.builtin.copy"`
}

// CopyModuleArgs encapsulates ansible copy module args
type CopyModuleArgs struct {
	Src       string `yaml:"src,omitempty"`
	RemoteSrc string `yaml:"remote_src,omitempty"`
	Dest      string `yaml:"dest,omitempty"`
	Owner     string `yaml:"owner,omitempty"`
	Group     string `yaml:"group,omitempty"`
	Mode      string `yaml:"mode,omitempty"`
	Content   string `yaml:"content,omitempty"`
	Follow    string `yaml:"follow,omitempty"`
	Backup    string `yaml:"backup,omitempty"`
	Validate  string `yaml:"validate,omitempty"`
}

Im actually working on making this more generic so each module doesn't need dedicated structs.

from go-ansible.

apenella avatar apenella commented on May 28, 2024

Hi @lingxiao26!
Did you have the chance to validate the example again or review the suggestion made by @rhugga?

from go-ansible.

apenella avatar apenella commented on May 28, 2024

I will close the issue due to inactivity. Feel free to open a new issue if you need further assistance!
Thank you!

from go-ansible.

Related Issues (20)

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.