Closed lingxiao26 closed 7 months ago
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
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.
Hi @lingxiao26! Did you have the chance to validate the example again or review the suggestion made by @rhugga?
I will close the issue due to inactivity. Feel free to open a new issue if you need further assistance! Thank you!
examples/ansibleplaybook-simple-embedfs have a example. But it just like move files to a temporary directory. It's not embed files to binary after build