ansible (include_vars) 来自 include

ansible (include_vars) from include

正在从 Unable to open shell: Ansible v2.3.1.0

升级

我运行成,

[DEPRECATION WARNING]: include is kept for backwards compatibility but usage is discouraged. The module documentation details page may explain more about this rationale.. This feature will be removed in a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

来自

---
- hosts: ios
  gather_facts: no
  connection: local

  tasks:

  - name: obtain login credentials
    include_vars: secrets.yml

  - name: define provider
    set_fact:
      provider:
        host: "{{ inventory_hostname }}"
        username: "{{ creds['username'] }}"
        password: "{{ creds['password'] }}"
        #Uncomment next line if enable password is needed
        #auth_pass: "{{ creds['auth_pass'] }}"
        transport: cli

  - include: tasks/ios_command-freeform.yml

使用include_vars涉及文件夹内容的正确方法是什么? (尝试使用它,但 yml-s inside "tasks" 最终被主要游戏忽略)。

提前致谢

[root@ymlhost-3 ansible-yml]# cat cisco-play.yml 
---
- name: cisco-yml
  hosts: cisco
  gather_facts: no
  connection: local

  tasks:

  - name: obtain login credentials
    include_vars: secrets.yml

  - name: define provider
    set_fact:
      provider:
        host: "{{ inventory_hostname }}"
        username: "{{ creds['username'] }}"
        password: "{{ creds['password'] }}"
        auth_pass: "{{ creds['auth_pass'] }}"
        authorize: yes

  - name: Include all .yml
    include_vars:
      dir: 'tasks'
      extensions:
          - json
          - yml
[root@ymlhost-3 ansible-yml]# 

也尝试了不同的形式

[root@ymlhost-3 ansible-yml]# cat cisco-play.yml 
---
- name: cisco-yml
  hosts: cisco
  gather_facts: no
  connection: local

  tasks:

  - name: obtain login credentials
    include_vars: secrets.yml

  - name: define provider
    set_fact:
      provider:
        host: "{{ inventory_hostname }}"
        username: "{{ creds['username'] }}"
        password: "{{ creds['password'] }}"
        auth_pass: "{{ creds['auth_pass'] }}"
        authorize: yes

  - name: Include all .yml files except bastion.yml (2.3)
    include_vars:
      dir: 'vars'
      ignore_files: 'bastion.yml'
      extensions: ['yml']
[root@ymlhost-3 ansible-yml]# 

Ansible 告诉您 include 指令已被弃用,在未来的版本中将不起作用,reference:

include - include a play or task list.

DEPRECATED

The include action was too confusing, dealing with both plays and tasks, being both dynamic and static. This module will be removed in version 2.8. As alternatives use include_tasks, import_playbook, import_tasks.


替换:

- include: tasks/ios_command-freeform.yml

有:

- import_tasks: tasks/ios_command-freeform.yml

或:

- include_tasks: tasks/ios_command-freeform.yml

差异解释如下:Differences Between Static and Dynamic。您的用例可能没有区别,因此请选择 import_tasks.