在 Ansible 中设置和使用环境变量

Din*_*kar 3 ansible ansible-2.x ansible-inventory

我想在 Ansible 中设置一个值作为环境变量,然后在另一个剧本中使用它。以下是我的剧本:

获取cmd.yaml

[root@a6296ab33a34 test_code]# vi get-cwd.yaml 

- hosts: localhost
  connection: local
  gather_facts: False

  tasks:

  #- name: Get directory
  #  shell: export ACWD="{{ playbook_dir }}"
  #  when: platform == 'jenkins'

  - name: Get CWD
    shell: "export ACWD=/test_code_demo"
    when: platform != 'jenkins'

  - name: DEMO
    shell: echo $ACWD
Run Code Online (Sandbox Code Playgroud)

输出

[root@a6296ab33a34 test_code]# vi get-cwd.yaml 
[root@a6296ab33a34 test_code]# ansible-playbook get-cwd.yaml --extra-vars="@deploy-vars.yaml" -vv
 [WARNING] Ansible is being run in a world writable directory (/test_code), ignoring it as an ansible.cfg source. For more information see https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
ansible-playbook 2.8.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, Jun 20 2019, 20:27:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Using /etc/ansible/ansible.cfg as config file
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAYBOOK: get-cwd.yaml *********************************************************************************************************************************************************************************************************************
1 plays in get-cwd.yaml

PLAY [localhost] ***************************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [Get CWD] *****************************************************************************************************************************************************************************************************************************
task path: /test_code/get-cwd.yaml:11
changed: [localhost] => {"changed": true, "cmd": "export ACWD=/test_code_demo", "delta": "0:00:00.713703", "end": "2019-12-13 14:43:37.054390", "rc": 0, "start": "2019-12-13 14:43:36.340687", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [DEMO] ********************************************************************************************************************************************************************************************************************************
task path: /test_code/get-cwd.yaml:15
changed: [localhost] => {"changed": true, "cmd": "echo $ACWD", "delta": "0:00:00.705605", "end": "2019-12-13 14:43:37.919962", "rc": 0, "start": "2019-12-13 14:43:37.214357", "stderr": "", "stderr_lines": [], "stdout": "/test_code_dinesh", "stdout_lines": ["/test_code_dinesh"]}
META: ran handlers
META: ran handlers

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

[root@a6296ab33a34 test_code]#
Run Code Online (Sandbox Code Playgroud)

您可以看到,尽管我尝试将值设置为test_code_demo,但旧值test_code_dinesh仍在反映。

请让我知道解决上述问题的方法。

lar*_*sks 5

请记住,当您设置环境变量(任何地方,而不仅仅是在 Ansible 中)时,它只会影响当前进程及其子进程。

当你运行这样的东西时:

  - name: Get CWD
    shell: "export ACWD=/test_code_demo"
    when: platform != 'jenkins'
Run Code Online (Sandbox Code Playgroud)

你是:

  1. 生成贝壳
  2. ACWD在该 shell 中设置环境变量
  3. 退出外壳

至此,环境就被破坏了。无法在一项任务中设置环境变量并使其影响另一项任务。您可以environment使用任务上的键设置每个任务的环境变量,如下所示:

  - name: DEMO
    shell: echo $ACWD
    environment:
      ACWD: '/test_code_demo'
Run Code Online (Sandbox Code Playgroud)

如果您需要将环境设置应用于多个任务,您可以将其设置为一次播放:

- hosts: localhost
  environment:
    ACWD: '/test_code_demo'
  tasks:
    - command: 'echo $ACWD'
      register: output1

    - command: 'echo $ACWD'
      register: output2

    - debug:
        msg:
          - "{{ output1.stdout }}"
          - "{{ output2.stdout }}"
Run Code Online (Sandbox Code Playgroud)