Chef cookbook not seeing overridden attributes in another cookbook - attributes

I have a run_list like this [ recipe["git_deploy"], recipe["my_role_cookbook"] ]
I set attributes that git_deploy needs in the default.rb recipe of my_role_cookbook. However, the git_deploy doesn't get these attributes.
If I put the attribute needed by git_deploy in my_role_cookbook/attributes/default.rb, then it works. If I put lazy evaluation in git_deploy - it works as well.
I don't get why it doesn't work if I use node.override in the my_rolecook_book/recipes/default.rb
The bigger picture: I have three environments, and in the role file of my_role, I have env_run_lists like this:
"production" => ["recipe[git_deploy]","recipe[my_role]"],
"staging" =>["recipe[git_deploy]","recipe[my_role]"],
"develop" => ["recipe[my_role]"]
EDIT
After using debug_value in both cookbooks:
When using lazy evaluation in git_deploy(node.override used in my_role_cookbook/recipe/default.rb):
Chef::Log output from git_deploy
[2015-07-31T11:44:12+00:00] FATAL: [["set_unless_enabled?", false], ["default", :not_present], ["env_default", :not_present], ["role_default", :not_present], ["force_default", :not_present], ["normal", :not_present], ["override", :not_present], ["role_override", :not_present], ["env_override", :not_present], ["force_override", :not_present], ["automatic", :not_present]]
LOG output from my_role_cookbook
[2015-07-31T11:44:12+00:00] FATAL: [["set_unless_enabled?", false], ["default", :not_present], ["env_default", :not_present], ["role_default", :not_present], ["force_default", :not_present], ["normal", :not_present], ["override", "youtube-minion"], ["role_override", :not_present], ["env_override", :not_present], ["force_override", :not_present], ["automatic", :not_present]]
When not using lazy evaluation(only node.override in my_role_cookbook)
LOG from git_deploy. Right after the log with debug_value, the git_deploy fails with undefined method `[]' for nil:NilClass
[2015-07-31T11:45:53+00:00] FATAL: [["set_unless_enabled?", false], ["default", :not_present], ["env_default", :not_present], ["role_default", :not_present], ["force_default", :not_present], ["normal", :not_present], ["override", :not_present], ["role_override", :not_present], ["env_override", :not_present], ["force_override", :not_present], ["automatic", :not_present]]
LOG from my_role_cookbook - this LOG is not shown, because the previous recipe fails.

All files inside attributes/* are merged and evaluated before any recipes run. Since you've put git_deploy first on your runlist, it's recipes run before yours. You must use attribute files in order to get your attributes injected before git deploy's recipes, and then your recipe, runs. Or put git_deploy as an include from your recipe.
Here's a link with more on how attribute files work.

Related

Ansible Molecule fails to create the docker instance

I am trying to get Molecule working with Docker on a Centos7 host.
The 'molecule create' command fails to create docker instance, and gives error: 'Unsupported parameters for (community.docker.docker_container) module: ....'
Checking with 'docker ps -a' confirms instance was not created.
I can confirm starting containers manually with 'docker run' works
fine.
It's only the molecule failing to start the container
I am stuck with this, and your help is highly appreciated
Here is the complete error message:
TASK [Create molecule instance(s)] *********************************************
changed: [localhost] => (item=centos7)
TASK [Wait for instance(s) creation to complete] *******************************
FAILED - RETRYING: Wait for instance(s) creation to complete (300 retries left).
failed: [localhost] (item={'started': 1, 'finished': 0, 'ansible_job_id': '353704502720.15356', 'results_file': '/home/guest/.ansible_async/353704502720.15356', 'changed': True, 'failed': False, 'item': {'image': 'test-runner', 'name': 'centos7', 'pre_build_image': True}, 'ansible_loop_var': 'item'}) => {"ansible_job_id": "353704502720.15356", "ansible_loop_var": "item", "attempts": 2, "changed": false, "finished": 1, "item": {"ansible_job_id": "353704502720.15356", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": {"image": "test-runner", "name": "centos7", "pre_build_image": true}, "results_file": "/home/guest/.ansible_async/353704502720.15356", "started": 1}, "msg": "Unsupported parameters for (community.docker.docker_container) module: command_handling Supported parameters include: api_version, auto_remove, blkio_weight, ca_cert, cap_drop, capabilities, cgroup_parent, cleanup, client_cert, client_key, command, comparisons, container_default_behavior, cpu_period, cpu_quota, cpu_shares, cpus, cpuset_cpus, cpuset_mems, debug, default_host_ip, detach, device_read_bps, device_read_iops, device_requests, device_write_bps, device_write_iops, devices, dns_opts, dns_search_domains, dns_servers, docker_host, domainname, entrypoint, env, env_file, etc_hosts, exposed_ports, force_kill, groups, healthcheck, hostname, ignore_image, image, init, interactive, ipc_mode, keep_volumes, kernel_memory, kill_signal, labels, links, log_driver, log_options, mac_address, memory, memory_reservation, memory_swap, memory_swappiness, mounts, name, network_mode, networks, networks_cli_compatible, oom_killer, oom_score_adj, output_logs, paused, pid_mode, pids_limit, privileged, published_ports, pull, purge_networks, read_only, recreate, removal_wait_timeout, restart, restart_policy, restart_retries, runtime, security_opts, shm_size, ssl_version, state, stop_signal, stop_timeout, sysctls, timeout, tls, tls_hostname, tmpfs, tty, ulimits, user, userns_mode, uts, validate_certs, volume_driver, volumes, volumes_from, working_dir", "stderr": "/tmp/ansible_community.docker.docker_container_payload_1djctes1/ansible_community.docker.docker_container_payload.zip/ansible_collections/community/docker/plugins/modules/docker_container.py:1193: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.\n", "stderr_lines": ["/tmp/ansible_community.docker.docker_container_payload_1djctes1/ansible_community.docker.docker_container_payload.zip/ansible_collections/community/docker/plugins/modules/docker_container.py:1193: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead."]}
$ pip list | egrep "mole|docker|ansible"
ansible 2.10.7
ansible-base 2.10.17
ansible-compat 2.2.0
ansible-core 2.12.0
docker 6.0.0
molecule 4.0.1
molecule-docker 2.0.0
$ docker --version
Docker version 20.10.14, build a224086
$ ansible --version
ansible 2.10.17
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/guest/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/guest/mole3/mole3/lib/python3.8/site-packages/ansible
executable location = /home/guest/mole3/mole3/bin/ansible
python version = 3.8.11 (default, Sep 1 2021, 12:33:46) [GCC 9.3.1 20200408 (Red Hat 9.3.1-2)]
$ molecule --version
molecule 4.0.1 using python 3.8
ansible:2.10.17
delegated:4.0.1 from molecule
docker:2.0.0 from molecule_docker requiring collections: community.docker>=3.0.0-a2
$ cat molecule/default/molecule.yml
---
dependency:
name: galaxy
enabled: False
driver:
name: docker
platforms:
- name: centos7
image: test-runner
pre_build_image: true
provisioner:
name: ansible
verifier:
name: ansible

Ansible: Jinja2 doesn't read dictionary object correctly

EDIT: I HAD THE WRONG FILE OPEN IN ATOM, CONTINUE TO MY ANSWER TO KNOW HOW.
I have a playbook that is supposed to spin up websites for three fictional company names, however in one of my Jinja2 templates, it doesn't find a variable that is clearly located in a dictionary. Any thoughts are greatly appreciated!
Ansible Version:
ansible 2.10.13
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Jan 27 2021, 01:17:18) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
playbook.yml
---
- hosts: company_servers
vars:
company_site:
totsuki:
companyname: UA High School
apache_dir: totsuki_academy
filename: totsuki_academy
companyid: 1
uahs:
companyname: UA High School
apache_dir: ua_high_school
filename: ua_highschool
companyid: 2
we:
companyname: Wayne Enterprises
apache_dir: wayne_enterprises
filename: ua_highschool
companyid: 3
tasks:
- name: Install Apache
dnf:
name: httpd
state: latest
- name: Create Website Folders
file:
path: /var/www/html/{{ item.value.apache_dir }}
state: directory
mode: 0775
with_dict: "{{company_site}}"
- name: Create HTML files
template:
src: htmltemplate2.html.j2
dest: /var/www/html/{{ item.value.apache_dir }}/index.html
owner: apache
group: apache
mode: 0775
with_dict: "{{company_site}}"
- name: Apache Preference Import
template:
src: httpd.conf
dest: /etc/httpd/conf/httpd.conf
mode: 0775
- name: Create Configuration Folders
file:
path: /etc/httpd/{{ item }}
state: directory
mode: 0775
loop:
- sites-enabled
- sites-available
- name: Apache Preference Import
template:
src: httpd.conf
dest: /etc/httpd/sites-available
mode: 0775
- name: Create a symbolic link
file:
src: /etc/httpd/sites-available
dest: /etc/httpd/sites-enabled
state: link
- name: Restart Apache
service:
name: httpd
state: restarted
- name: Stop Firewall
service:
name: firewalld
state: stopped
Infuriating Error:
The full traceback is:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1066, in do_template
res = j2_concat(rf)
File "<template>", line 14, in root
File "/usr/local/lib/python3.6/site-packages/jinja2/runtime.py", line 903, in _fail_with_undefined_error
raise self._undefined_exception(self._undefined_message)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'companyname'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/action/template.py", line 139, in run
resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1103, in do_template
raise AnsibleUndefinedVariable(e)
ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'
failed: [192.168.100.6] (item={'key': 'we', 'value': {'companyname': 'Wayne Enterprises', 'apache_dir': 'wayne_enterprises', 'filename': 'ua_highschool', 'companyid': 3}}) => {
"ansible_loop_var": "item",
"changed": false,
"item": {
"key": "we",
"value": {
"apache_dir": "wayne_enterprises",
"companyid": 3,
"companyname": "Wayne Enterprises",
"filename": "ua_highschool"
}
},
"msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'"
}
The full traceback is:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1066, in do_template
res = j2_concat(rf)
File "<template>", line 14, in root
File "/usr/local/lib/python3.6/site-packages/jinja2/runtime.py", line 903, in _fail_with_undefined_error
raise self._undefined_exception(self._undefined_message)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'companyname'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/ansible/plugins/action/template.py", line 139, in run
resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
File "/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py", line 1103, in do_template
raise AnsibleUndefinedVariable(e)
ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'
failed: [192.168.100.7] (item={'key': 'we', 'value': {'companyname': 'Wayne Enterprises', 'apache_dir': 'wayne_enterprises', 'filename': 'ua_highschool', 'companyid': 3}}) => {
"ansible_loop_var": "item",
"changed": false,
"item": {
"key": "we",
"value": {
"apache_dir": "wayne_enterprises",
"companyid": 3,
"companyname": "Wayne Enterprises",
"filename": "ua_highschool"
}
},
"msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'"
}
Also, I have noticed that Wayne enterprises 'filename' wasn't correct and I did adjust that.
It says object has no attribute 'companyname'
and here is my html.j2
<html>
<head>
<title>{{ item.value.companyname }}</title>
</head>
<body>
<h1>Welcome to the {{ item.value.companyname }} website</h1>
IP Address: {{ ansible_default_ipv4.address }}
</body>
</html>
This was solved by taking a look at the path of the file I had open in Atom, and comparing it to the path of the file that Ansible was running. It's important to know, that when you're using the template module in Ansible, that files you're referencing without an exact path should be stored in ~/templates and Ansible will make that folder for you upon running a playbook with the template module.
Common sense in my mind thought it might reference the directory of the playbook, but is not how Ansible works.

How to to globally disable E501 in VSCODE and pylama

I am using Visual Studio Code and pylama linter.
Currently I am added # noqa to every long line to avoid the following linter message:
line too long (100 > 79 characters) [pycodestyle]pylama(E501)
I've added "--disable=E501" to VSCODE's workspace settings.json file as shown below:
{
"editor.tabSize": 2,
"editor.detectIndentation": false,
"python.linting.enabled": true,
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": false,
"python.linting.pycodestyleEnabled": false,
"python.linting.pylamaEnabled": true,
"[python]": {
"editor.tabSize": 4
},
"python.linting.pylama": [
"--disable=E501"
]
}
but still I get E501.
How can I disable E501 in my VSCODE workspace for good?
For other linters, the .settings file seems to be looking for
python.linting.<linter>Args
so I recommend trying:
"python.linting.pylamaArgs": [
"--ignore=E501"
]
or potentially
python.linting.pylamaArgs": ["--disable=E501"]
See also: https://code.visualstudio.com/docs/python/settings-reference#_pylama
that seems to suggest the same:
pylamaArgs [] Additional arguments for pylama, where each top-level element that's separated by a space is a separate item in the list.

Jhipster import-jdl controlling what gets generated

I have a simple JHipster JDL file that looks like below
entity Student {
name String,
age Integer
}
and I have this saved in a file called student.jh
Now I am trying to make use of the JHipster CLI and generate the entities needed for my dummy project. I noticed that by default JHipster ends up generating the following:
Entity classes
DTO classes
Repository classes
Service classes (I think this we can further customise by adding options as to whether we need service implementation as well)
Rest Controllers
Integration tests
Gatling tests (This I believe is configurable)
I was hoping to know if I could have JHipster skip generating the rest controllers because I would like to add custom rest controllers by hand. I know that I could merely have these controllers alone deleted (along with the Integration Tests that also get generated).
Is there any way in which this can be controlled? I have tried using all of the cli options for jhipster import-jdl but no luck yet.
here's the command I am using
jhipster import-jdl src/main/resources/student.jh
Here's the complete output
jhipster -d import-jdl src/main/resources/student.jh
INFO! Using JHipster version installed globally
DEBUG! Executing CLI only script
DEBUG! cmd: import-jdl from ./import-jdl
DEBUG! args: [object Undefined]
INFO! Executing import-jdl src/main/resources/student.jh
DEBUG! Options: debug: true, skip-install: false, interactive: false, json-only: false, ignore-application: false, ignore-deployments: false, skip-ui-grouping: false, skip-db-changelog: false, skip-sample-repository: false, skipInstall: false, jsonOnly: false, ignoreApplication: false, ignoreDeployments: false, skipUiGrouping: false, skipDbChangelog: false, skipSampleRepository: false, from-cli: true, fromCli: true, inline:
DEBUG! JDLProcessor started with files: src/main/resources/student.jh and options: debug: true, skip-install: false, interactive: false, json-only: false, ignore-application: false, ignore-deployments: false, skip-ui-grouping: false, skip-db-changelog: false, skip-sample-repository: false, skipInstall: false, jsonOnly: false, ignoreApplication: false, ignoreDeployments: false, skipUiGrouping: false, skipDbChangelog: false, skipSampleRepository: false, from-cli: true, fromCli: true
INFO! Found .yo-rc.json on path. This is an existing app
INFO! The JDL is being parsed.
DEBUG! importState exportedEntities: 1
DEBUG! importState exportedApplications: 0
DEBUG! importState exportedDeployments: 0
INFO! Found entities: Student.
INFO! The JDL has been successfully parsed
DEBUG! Applications not generated
INFO! Generating 1 entity.
DEBUG! Generator is starting
Found the .jhipster/Student.json configuration file, entity can be automatically generated!
The entity Student is being updated.
DEBUG! Generator is starting
DEBUG! Time taken to write files: 62ms
create src/main/resources/config/liquibase/changelog/20210202113651_added_entity_Student.xml
create src/main/resources/config/liquibase/fake-data/student.csv
create src/main/java/org/rationaleemotions/domain/Student.java
create src/main/java/org/rationaleemotions/web/rest/StudentResource.java
create src/main/java/org/rationaleemotions/repository/search/StudentSearchRepository.java
create src/main/java/org/rationaleemotions/repository/StudentRepository.java
create src/test/java/org/rationaleemotions/web/rest/StudentResourceIT.java
create src/test/java/org/rationaleemotions/repository/search/StudentSearchRepositoryMockConfiguration.java
create src/test/java/org/rationaleemotions/domain/StudentTest.java
force src/main/resources/config/liquibase/master.xml
DEBUG! Generator has ended
DEBUG! Generator has ended
DEBUG! Deployments not generated
INFO! Congratulations, JHipster execution is complete!
Below is the complete output of my JHipster info command
jhipster info
INFO! Using JHipster version installed globally
INFO! Executing jhipster:info
Welcome to the JHipster Information Sub-Generator
##### **JHipster Version(s)**
##### **JHipster configuration, a `.yo-rc.json` file generated in the root folder**
<details>
<summary>.yo-rc.json file</summary>
<pre>
{
"generator-jhipster": {
"promptValues": {
"packageName": "org.rationaleemotions"
},
"jhipsterVersion": "6.10.5",
"applicationType": "microservice",
"baseName": "necropolis",
"packageName": "org.rationaleemotions",
"packageFolder": "org/rationaleemotions",
"serverPort": "8081",
"authenticationType": "oauth2",
"cacheProvider": "hazelcast",
"enableHibernateCache": true,
"websocket": false,
"databaseType": "sql",
"devDatabaseType": "mariadb",
"prodDatabaseType": "mariadb",
"searchEngine": "elasticsearch",
"messageBroker": false,
"serviceDiscoveryType": "eureka",
"buildTool": "maven",
"enableSwaggerCodegen": false,
"jwtSecretKey": "YourJWTSecretKeyWasReplacedByThisMeaninglessTextByTheJHipsterInfoCommandForObviousSecurityReasons",
"embeddableLaunchScript": false,
"creationTimestamp": 1611077969383,
"testFrameworks": [],
"jhiPrefix": "jhi",
"entitySuffix": "",
"dtoSuffix": "DTO",
"otherModules": [],
"enableTranslation": false,
"clientPackageManager": "npm",
"blueprints": [],
"skipClient": true,
"skipUserManagement": true
}
}
</pre>
</details>
##### **JDL for the Entity configuration(s) `entityName.json` files generated in the `.jhipster` directory**
<details>
<summary>JDL entity definitions</summary>
<pre>
entity Student {
name String,
age Integer
}
microservice Student with necropolis
clientRootFolder Student with necropolis
</pre>
</details>
##### **Environment and Tools**
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
git version 2.30.0
node: v14.15.3
yeoman: 3.1.1
Docker version 20.10.0, build 7287ab3
docker-compose version 1.27.4, build 40524192
identical .jhipster/Student.json
INFO! Congratulations, JHipster execution is complete!

Ansible: Unexpected templating type error: expected string or buffer

I have a register with the following contents:
ok: [hostname] => {
"changed": false,
"msg": {
"changed": true,
"cmd": "cd /tmp\n ./status.sh dev",
"delta": "0:00:00.023660",
"end": "2018-11-28 17:46:05.838934",
"rc": 0,
"start": "2018-11-28 17:46:05.815274",
"stderr": "",
"stderr_lines": [],
"stdout": "application is not running. no pid file found",
"stdout_lines": [
"application is not running. no pid file found"
]
}
}
When i see the substring "not" in the register's stdout, i want to trigger another task:
- name: Starting Application As Requested
shell: /tmp/start.sh
when: operation_status.stdout | search('not')
However, i see this error in my triggered task
fatal: [host]: FAILED! => {
"failed": true,
"msg": "The conditional check 'operation_status.stdout | search('not')' failed. The error was: Unexpected templating type error occurred on ({% if operation_status.stdout | search('not') %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/path/to/ansible_playbook.yml': line 46, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Starting Application As Requested\n ^ here\n"
I only see this error when adding the when condition. Without it, my playbook succeeds. What am i doing wrong here?
Version details:
ansible 2.3.0.0
python version = 2.6.6 (r266:84292, Aug 9 2016, 06:11:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
This error occurs when the variable (in your case operation_status.stdout) is undefined. Maybe the status.sh script doesn't write to stdout when the service is running.
Can you put a debug task and print the value of this variable before the task with "when"?
- name: Debug print value of operation_status.stdout
debug:var=operation_status.stdout
You can also try and modify the when condition to be:
- name: Starting Application As Requested
shell: /tmp/start.sh
when: "'not' in operation_status.stdout"

Resources