mocart2

https://stackoverflow.com/questions/77324457/generate-valid-dictionary-in-ansible-with-condition-dep

Oct 20th, 2023
1,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 4.11 KB | None | 0 0
  1. - name: test_playbook
  2.   hosts: localhost
  3.   connection: local
  4.  
  5.   vars:
  6.     server__fqdn: myserver.local
  7.     server__team: superduper_team
  8.  
  9.     request_body:
  10.       matchers: |
  11.        {% filter from_yaml %}
  12.         {% if "myserver" in server__fqdn %}
  13.         - name: "host"
  14.           value: "{{ server__fqdn }}"
  15.           type: "EQUAL"
  16.         - name: "node"
  17.           value: "myserver"
  18.           type: "EQUAL"
  19.         {% else %}
  20.         - name: "host"
  21.           value: "{{ server__fqdn }}"
  22.           type: "EQUAL"
  23.         {% endif %}
  24.         {% endfilter %}
  25.       teamName: "{{ server__team }}"
  26.  
  27.   tasks:
  28.     - debug:
  29.         var: request_body|type_debug
  30.     - debug:
  31.         var: request_body
  32.     - debug:
  33.         var: request_body|to_json
  34.        
  35.        
  36.  
  37. OUTPUT:
  38. ansible-playbook playbooks/test.yml
  39. /usr/local/lib/python3.7/site-packages/paramiko/transport.py:219: CryptographyDeprecationWarning: Blowfish has been deprecated
  40.   "class": algorithms.Blowfish,
  41. [WARNING]: No inventory was parsed, only implicit localhost is available
  42.  
  43. PLAY [test_playbook] ****************************************************************************************************************************
  44.  
  45. TASK [Gathering Facts] **************************************************************************************************************************
  46. пятница 20 октября 2023  16:30:14 +0300 (0:00:00.013)       0:00:00.013 *******
  47. ok: [localhost]
  48.  
  49. TASK [debug] ************************************************************************************************************************************
  50. пятница 20 октября 2023  16:30:15 +0300 (0:00:00.911)       0:00:00.924 *******
  51. fatal: [localhost]: FAILED! =>
  52.   msg: |-
  53.     An unhandled exception occurred while templating '{'matchers': '{% filter from_yaml %}\n{% if "myserver" in server__fqdn %}\n- name: "host"\n  value: "{{ server__fqdn }}"\n  type: "EQUAL"\n- name: "node"\n  value: "myserver"\n  type: "EQUAL"\n{% else %}\n- name: "host"\n  value: "{{ server__fqdn }}"\n  type: "EQUAL"\n{% endif %}\n{% endfilter %}\n', 'teamName': '{{ server__team }}'}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: Unexpected templating type error occurred on ({% filter from_yaml %}
  54.     {% if "myserver" in server__fqdn %}
  55.     - name: "host"
  56.       value: "{{ server__fqdn }}"
  57.       type: "EQUAL"
  58.     - name: "node"
  59.       value: "myserver"
  60.       type: "EQUAL"
  61.     {% else %}
  62.     - name: "host"
  63.       value: "{{ server__fqdn }}"
  64.       type: "EQUAL"
  65.     {% endif %}
  66.     {% endfilter %}
  67.     ): sequence item 0: expected str instance, list found
  68.  
  69. PLAY RECAP **************************************************************************************************************************************
  70. localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
  71.  
  72. пятница 20 октября 2023  16:30:15 +0300 (0:00:00.040)       0:00:00.965 *******
  73. ===============================================================================
  74. Gathering Facts -------------------------------------------------------------------------------------------------------------------------- 0.91s
  75. debug ------------------------------------------------------------------------------------------------------------------------------------ 0.04s
  76.  
  77.  
  78. ansible --version                                              
  79. /usr/local/lib/python3.7/site-packages/paramiko/transport.py:219: CryptographyDeprecationWarning: Blowfish has been deprecated
  80.   "class": algorithms.Blowfish,
  81. ansible [core 2.11.12]
  82.   config file = /Users/username/projects/ansible.cfg
  83.   configured module search path = ['/Users/username/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  84.   ansible python module location = /usr/local/lib/python3.7/site-packages/ansible
  85.   ansible collection location = /Users/username/projects:/Users/username/.ansible/collections
  86.   executable location = /usr/local/bin/ansible
  87.   python version = 3.7.12 (default, Oct 22 2021, 17:50:30) [Clang 13.0.0 (clang-1300.0.29.3)]
  88.   jinja version = 3.1.2
  89.   libyaml = True
Tags: ansible
Advertisement
Add Comment
Please, Sign In to add comment