visible加载zshenv并在另一个任务中使用ENV



我想加载一个zshenv文件(使用source命令),然后在另一个任务中使用env。

这是我有的。我希望有更好的解决办法

<标题>文件

目录结构
.
├── ansible.cfg
├── hosts.yaml
├── profiles
│   └── macos.yaml
├── roles
│   └── base
│       ├── tasks
│       │   ├── git.yaml
│       │   └── main.yaml
│       └── vars
└── tools
└── zsh
└── .zshenv

。/ansible.cfg

[defaults]
inventory = ./hosts.yaml
roles_path = ./roles/
stdout_callback = yaml

。/hosts.yaml

---
all:
hosts:
localhost

/profiles/macos.yaml。

---
# run MacOS configs
# - hosts: localhost
#   connection: local
#   tags: macos
#   roles:
#     - macos
#     # when: ansible_distribution == "MacOSX"
- hosts: localhost
connection: local
tags: base
roles:
- base

。/角色/基地/main.yaml

---
- import_tasks: tasks/git.yaml

。/角色/基地/git.yaml

---
- name: source zshenv
shell:
cmd: source ../tools/zsh/.zshenv; echo $GIT_CONFIG_PATH
register: gitConfigPath
- name: Link gitconfig file
file:
# PWD: ./profiles
src: "{{ ansible_env.PWD }}/../tools/git/.gitconfig"
dest: "{{ gitConfigPath.stdout }}"
state: link
# - name: print ansible_env
#   debug:
#     msg: "{{ ansible_env }}"
#
# - name: print gitConfigPath
#   debug:
#     msg: "{{ gitConfigPath.stdout }}"
#

。/工具/zsh/.zshenv

export XDG_CONFIG_HOME="$HOME/.config"
export GIT_CONFIG_PATH="$XDG_CONFIG_HOME/git/config"

命令运行

ansible-playbook profiles/macos.yaml -v

PS:在visible

中这样做会更容易source tools/zsh/.zshenv && ansible-playbook profiles/macos.yaml -v

给出没有角色的简化项目

shell> tree -a .
.
├── ansible.cfg
├── hosts
├── pb.yml
└── tools
├── git
└── zsh
└── .zshenv
shell> cat hosts
localhost
shell> cat tools/zsh/.zshenv 
export GIT_CONFIG_PATH=/home/admin/git/.gitconfig
export ENV1=env1
export ENV2=env2
export ENV3=env3
eval "$(direnv hook zsh)"

自己解析环境。例如

zshenv: "{{ dict(lookup('file', 'tools/zsh/.zshenv').splitlines()|
select('match', '^\s*export .*$')|
map('regex_replace', '^\s*export\s+', '')|
map('split', '=')) }}"

zshenv:
ENV1: env1
ENV2: env2
ENV3: env3
GIT_CONFIG_PATH: /home/admin/git/.gitconfig
然后,使用字典zshenv
- name: Link gitconfig file
file:
dest: "{{ playbook_dir }}/tools/git/.gitconfig"
src: "{{ zshenv.GIT_CONFIG_PATH }}"
state: link

给出了——check——diff选项

TASK [Link gitconfig file] *******************************************************************
--- before
+++ after
@@ -1,2 +1,2 @@
path: /export/scratch/tmp7/test-116/tools/git/.gitconfig
-state: absent
+state: link
changed: [localhost]

Notes

  1. 测试
  2. 的完整剧本示例
shell> cat pb.yml
- hosts: localhost
vars:
zshenv: "{{ dict(lookup('file', 'tools/zsh/.zshenv').splitlines()|
select('match', '^\s*export .*$')|
map('regex_replace', '^\s*export\s+', '')|
map('split', '=')) }}"
tasks:
- debug:
var: zshenv
- name: Link gitconfig file
file:
dest: "{{ playbook_dir }}/tools/git/.gitconfig"
src: "{{ zshenv.GIT_CONFIG_PATH }}"
state: link

shell> ansible-playbook pb.yml
PLAY [localhost] *****************************************************************************
TASK [debug] *********************************************************************************
ok: [localhost] => 
zshenv:
ENV1: env1
ENV2: env2
ENV3: env3
GIT_CONFIG_PATH: /home/admin/git/.gitconfig
TASK [Link gitconfig file] *******************************************************************
changed: [localhost]
PLAY RECAP ***********************************************************************************
localhost: ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

链接tools/git/.gitconfig -> /home/admin/git/.gitconfig创建成功

shell> tree -a .
.
├── ansible.cfg
├── hosts
├── pb.yml
└── tools
├── git
│   └── .gitconfig -> /home/admin/git/.gitconfig
└── zsh
└── .zshenv
  1. 您可以使用字典zshenv设置环境。例如,
- command: echo $GIT_CONFIG_PATH
environment: "{{ zshenv }}"
register: out
- debug:
var: out.stdout

out.stdout: /home/admin/git/.gitconfig
  1. 如果您想在整个游戏中全局使用此环境,则缓存字典。例如,
shell> grep fact_caching ansible.cfg
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_cache
fact_caching_prefix = ansible_facts_
fact_caching_timeout = 86400
- set_fact:
zshenv: "{{ dict(lookup('file', 'tools/zsh/.zshenv').splitlines()|
select('match', '^\s*export .*$')|
map('regex_replace', '^\s*export\s+', '')|
map('split', '=')) }}"
cacheable: true

,

- hosts: localhost
environment: "{{ zshenv }}"
tasks:
- command: echo $GIT_CONFIG_PATH
register: out
- debug:
var: out.stdout

PLAY [localhost] *****************************************************************************
TASK [command] *******************************************************************************
changed: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] => 
out.stdout: /home/admin/git/.gitconfig
PLAY RECAP ***********************************************************************************
localhost: ok=7    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

在Vladimir Botka的帮助下,使用https://stackoverflow.com/a/74924664/3053548的答案

我修改了一些他的代码

<标题>TLDR h1> li>来源zshenv文件
  • 打印shell会话中的所有ENV
  • 将输出存储为可见事实
  • 访问任务中的ENV
  • 文件

    /profiles/macos.yaml。

    ---
    # run MacOS configs
    - hosts: localhost
    connection: local
    tags: always
    tasks:
    - name: source zshenv
    shell:
    cmd: source ../tools/zsh/.zshenv; env
    register: out
    changed_when: false
    - name: store zshenv as fact
    set_fact:
    zshenv: "{{ dict(out.stdout.splitlines() | map('split', '='))  }}"
    changed_when: false
    # - hosts: localhost
    #   connection: local
    #   tags: macos
    #   roles:
    #     - macos
    #     # when: ansible_distribution == "MacOSX"
    - hosts: localhost
    connection: local
    tags: base
    roles:
    - base
    

    。/角色/基地/任务/git.yaml

    ---
    - name: Link gitconfig file
    file:
    src: "{{ ansible_env.PWD }}/../tools/git/.gitconfig"
    dest: "{{ zshenv.GIT_CONFIG_PATH }}"
    state: link
    

    命令运行

    ansible-playbook profiles/macos.yaml

    最新更新