Ansible not able to create symlink - linux

I'm trying to create a symlink and I'm not able to solve this error . Please suggest me a solution on how to solve this error
Code: Creating a symlink for /usr/local/bin/terraform-env/bin/* in folder /usr/local/bin
I tried with /usr/local/bin/ (with and without slash)
- name: Move tfenv file:
src: "/usr/local/bin/terraform-env/bin/{{ item.src }}"
dest: "/usr/local/bin/"
state: link
owner: root
group: root
mode: 755
force: yes
with_items:
- src: terraform
- src: tfenv
TASK [terraform : Move tfenv] **************************************************
task path: /opt/ansible/roles/terraform/tasks/main.yml:16
failed: [127.0.0.1] (item={'src': 'terraform'}) => {"changed": false, "gid": 0, "group": "root", "item": {"src": "terraform"}, "mode": "0755", "msg": "the directory /usr/local/bin/ is not empty, refusing to convert it", "owner": "root", "path": "/usr/local/bin/", "size": 4096, "state": "directory", "uid": 0}
failed: [127.0.0.1] (item={'src': 'tfenv'}) => {"changed": false, "gid": 0, "group": "root", "item": {"src": "tfenv"}, "mode": "0755", "msg": "the directory /usr/local/bin/ is not empty, refusing to convert it", "owner": "root", "path": "/usr/local/bin/", "size": 4096, "state": "directory", "uid": 0}
Using ansible 2.8.3

the directory /usr/local/bin/ is not empty, refusing to convert it
You are trying to create the symlink directly on the existing directory rather than creating an entry inside that dir to support the symlink. The following corrected task should get you going:
- name: Move tfenv file:
src: "/usr/local/bin/terraform-env/bin/{{ item.src }}"
dest: "/usr/local/bin/{{ item.src }}"
state: link
owner: root
group: root
mode: 755
force: yes
with_items:
- src: terraform
- src: tfenv

Related

du: cannot access ‘/home/*’: No such file or directory

I'm trying to create an ansible playbook to check disk utilization of each users in the /home/ directory
---
- name: User Home Directory Stat
hosts: linux7
become: true
vars:
directory: /home/*
tasks:
- name: Check available user directory and disk utilization
ansible.builtin.command: "sudo du -sh {{ directory }} "
register: msg
- debug:
var: msg.stdout_lines
However, i get
FAILED! => {"changed": true, "cmd": ["sudo", "du", "-sh", "/home/*"], "delta": "0:00:00.018716", "end": "2022-09-29 10:44:06.375725", "msg": "non-zero return code", "rc": 1, "start": "2022-09-29 10:44:06.357009", "stderr": "du: cannot access ‘/home/*’: No such file or directory", "stderr_lines": ["du: cannot access ‘/home/*’: No such file or directory"], "stdout": "", "stdout_lines": []}

shell command with Ansible playbook doesn't work

I have added to my playbook a small task that should change umask on my linux machine:
- name: set umask to 0022
shell: umask 0022
When running the playbook, I can see this task passes successfully:
changed: [myHostName] => {
"changed": true,
"cmd": "umask 0022",
"delta": "0:00:00.004660",
"end": "2020-08-04 16:28:44.153261",
"invocation": {
"module_args": {
"_raw_params": "umask 0022",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"rc": 0,
"start": "2020-08-04 16:28:44.148601",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
but After the playbook finishes, I check the umask and see that it was not changed at all:
-bash-4.2$ umask
0044
I also put a debug in my playbook right after the task I showed above, and the debug also shows that the umask was not changed..
Tried also with
become: yes
But got the same result..
When I do the command on my Linux manually, it will work:
-bash-4.2$ umask 0022
-bash-4.2$ umask
0022
Q: After the playbook finishes, I check the umask and see that it was not changed at all.
A: This is correct. Ansible isn't really doing things through the shell i.e. the changes live in this one session only.

Ansible file module error - chown failed: failed to look up user

I am trying to change the owner of a file using file module. I tried this piece of code:
---
- hosts: super_group
remote_user: ec2-user
tasks:
- name: Checking the user name
shell: /usr/bin/whoami
register: username
- name: Debugging the whoami username
debug: msg={{ username }}
- name: Changing the owner of a file
file: path=/home/ec2-user/test owner={{ username }}
Error:
TASK [Changing the owner of a file] ********************************************
fatal: [test]: FAILED! => {"changed": false, "failed": true, "gid": 0, "group": "root", "mode": "0644", "msg": "chown failed: failed to look up user {'stderr_lines': [], 'changed': True, 'end': '2017-07-10 01:49:11.495709', 'stdout': 'ec2-user', 'cmd': '/usr/bin/whoami', 'start': '2017-07-10 01:49:11.492286', 'delta': '0:00:00.003423', 'stderr': '', 'rc': 0, 'stdout_lines': ['ec2-user']}", "owner": "ec2-user", "path": "/home/ec2-user/test", "secontext": "unconfined_u:object_r:user_home_t:s0", "size": 0, "state": "file", "uid": 1000}
to retry, use: --limit #/home/ec2-user/ansible/test.retry
debug module is giving me this output:
TASK [Debugging the whoami username] *******************************************
ok: [test] => {
"msg": {
"changed": true,
"cmd": "/usr/bin/whoami",
"delta": "0:00:00.003423",
"end": "2017-07-10 01:49:11.495709",
"rc": 0,
"start": "2017-07-10 01:49:11.492286",
"stderr": "",
"stderr_lines": [],
"stdout": "ec2-user",
"stdout_lines": [
"ec2-user"
]
}
}
Note:
If I hardcode the value of username then it works fine:
- name: Changing the owner of a file
file: path=/home/ec2-user/test owner=ec2-user
Please let me know how to resolve this issue.
There is no issue. You want to use username.stdout, not username.
Please check the value you printed with the debug module and use reasoning.

Using register with a loop in Ansible

i want to code a playbook which IF a user exists changes the pw of it.
The playbook should be able to take n User's and change the pw of those Users.
At the moment im having the issue that the when is empty due to the loop, i tried using with_items: {{ user_exists.results }} but this is somehow not working.
(http://docs.ansible.com/ansible/playbooks_loops.html#using-register-with-a-loop)
Am i doing something wrong ?
Br,
Numblesix
---
-
become: true
become_method: sudo
hosts: xetest
name: "Updates the password of given User if exists"
tasks:
-
ignore_errors: true
name: "Check if User exists"
register: user_exists
shell: "grep -q {{ item.key }} /etc/passwd &>/dev/null"
with_dict: "{{ users }}"
-
debug:
var: user_exists
-
debug:
msg: "User name is {{ item.key }} and hash is {{ item.value.passwd}} and return code is: "
with_dict: "{{ users }}"
-
debug:
var: user_exists
with_items: "{{user_exists.results }}"
-
name: "updating password for given User"
user: "name={{ item.key }} update_password=always password={{ item.value.passwd}} createhome=no"
when: user_exists.rc == 0
with_dict: "{{ users }}"
with_items: "{{ user_exists.results }}"
vars:
users:
foo:
passwd: $6$random_salt$12A.ar9eNDsgmds3leKoCDZPmq7OHLvhBtQg/Q3K2G/3yeEa/r8Ou4DxJpN6vzccewugvZt7IkfCbHFF2i.QU.
RESULTS IN ERROR!
duplicate loop in task: items
WITHOUT with_items: "{{ user_exists.results }}" im getting this error
"failed": true, "msg": "The conditional check 'user_exists.rc == 0' failed.
The error was: error while evaluating conditional (user_exists.rc == 0):
'dict object' has no attribute 'rc'
For my testing, I'm using ansible 2.1.4.0.
When running the script, you can see in the debug for user_exists.results that it contains the input value passed in along with the return code:
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "grep -q foo /etc/passwd",
"delta": "0:00:00.009034",
"end": "2017-05-02 17:42:57.835871",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "grep -q foo /etc/passwd",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": {
"key": "foo",
"value": {
"passwd": "foobar"
}
},
"rc": 1,
"start": "2017-05-02 17:42:57.826837",
"stderr": "",
"stdout": "",
"stdout_lines": [],
"warnings": []
},
So instead doing two loops (which would have been done with with_nested and two lists), you can do everything with a single loop:
- name: "updating password for given User"
debug:
msg: "name={{ item.item.key }} update_password=always password={{ item.item.value.passwd}} createhome=no"
when: item.rc == 0
with_items: "{{ user_exists.results }}"
Note: In my testing shell: "grep -q {{ item.key }} /etc/passwd &>/dev/null" was always returning a 0 return code. I had to remove the "&>/dev/null" part to get the proper return code.

Invalid (corrupted) package.json file after npm install in a vagrant box

I have been trying to fix a vagrant setup for a laravel project.
Context
Host: Mac
Guest: Ubuntu 16.04
versions:
$ npm --version
2.15.9
$ node --version
v4.5.0
Problem
When I run $ npm install some of the dependency packages (apparently randomly) come with invalid characters and $ gulp fails.
To be more precise, the end of the corrupted file shows some invalid characters:
$ gulp
module.js:85
throw e;
^
SyntaxError: Error parsing /vagrant/node_modules/gulp-bower/node_modules/bower/lib/node_modules/graceful-fs/package.json: Unexpected token
And if you go to the file you will see, in the pointed line, a lot of weird characters.
But, as I said, the invalid dependency package varies every time I rm -Rf node_modules and then run a $ npm install again.
For the second time, for instance, I've got this:
$gulp
module.js:85
throw e;
^
SyntaxError: Error parsing /vagrant/node_modules/gulp-bower/node_modules/bower/lib/node_modules/configstore/package.json: Unexpected token
The invalid package.json file looks like this:
{
"_args": [
[
"configstore#^2.0.0",
"/private/var/folders/22/xz6_9gpx3jggts_8j68_25g80000gn/T/tmp-51023WwSVKpwQ7KvH"
]
],
"_from": "configstore#>=2.0.0 <3.0.0",
"_id": "configstore#2.0.0",
"_inCache": true,
"_installable": true,
"_location": "/configstore",
"_nodeVersion": "4.3.0",
"_npmOperationalInternal": {
"host": "packages-5-east.internal.npmjs.com",
"tmp": "tmp/configstore-2.0.0.tgz_1456822157166_0.897884224774316"
},
"_npmUser": {
"email": "sindresorhus#gmail.com",
"name": "sindresorhus"
},
"_npmVersion": "2.14.12",
"_phantomChildren": {},
"_requested": {
"name": "configstore",
"raw": "configstore#^2.0.0",
"rawSpec": "^2.0.0",
"scope": null,
"spec": ">=2.0.0 <3.0.0",
"type": "range"
},
"_requiredBy": [
"/",
"/update-notifier"
],
"_resolved": "https://registry.npmjs.org/configstore/-/configstore-2.0.0.tgz",
"_shasum": "8d81e9cdfa73ebd0e06bc985147856b2f1c4e764",
"_shrinkwrap": null,
"_spec": "configstore#^2.0.0",
"_where": "/private/var/folders/22/xz6_9gpx3jggts_8j68_25g80000gn/T/tmp-51023WwSVKpwQ7KvH",
"author": {
"email": "sindresorhus#gmail.com",
"name": "Sindre Sorhus",
"url": "sindresorhus.com"
},
"bugs" Bp6��5�f5���Ip6���G�9Y��G�9)F[���т]�=ٞp6�1AP��1AP���#��yAP���v�nY��G�91AP���AP��ٞp6�1AP��1AP���#��yAP���v�nY��G�91AP���AP�� Bp6��G�9yAP���Ip6��>6���ys7)F[��I�Mq8�6�f5I�Mq8�Ip6� ��ys7�:�ys7)F[���6�f5Q5��~
In�f5q5��~
a��G�99Sp6�P7/
AIp6�dpackage/library/fn/array/virtual/reduce.js�iCp6��y��G�9�^�.1AP��1AP�����f5���f5Y�O{�AP��}^�.1AP��1AP�����f5Y�O{�AP���AP����f��1AP��1AP�����f5Y�O{�AP���AP����f��1AP��1AP�����f5���f5Y�O{�AP��AIp6�(db3204cd5a9de2e6cd890b85c6e2f66bcf4f620aAIp6�inflight#>=1.0.4 <2.0.0AIp6�3.9.1AIp6�5.10.1�s��'1AP��1AP��y�G�9��G�9�AP���AP��a�f��1AP��1AP�����f5���f5Y�O{�AP���f��I��O{��G�9���'1AP��1AP����G�99�G�9�AP���AP����`��1AP��1AP��Y�O{�AP���AP���AP��AIp6�8https://registry.npmjs.org/inflight/-/inflight-1.0.5.tgzAIp6�ERROR: No README data found!/nAIp6�� has}
aMp6�Z�gu�
Mp6�45��G�91 Y�� Bp6�Y>�f5���Ip6���G�9I��G�9
If I run $ npm install from the host machine, the packages are downloaded correctly and $ gulp runs just fine.
My Configuration
I don't believe that my configuration is going to be relevant because the commands were executed after a $ vagrant ssh but, here they are:
Vagrantfile
Vagrant.configure(2) do |config|
config.vm.box = "geerlingguy/ubuntu1604"
config.vm.synced_folder "../laravel", "/vagrant",
owner: "vagrant",
group: "www-data",
mount_options: ["dmode=775,fmode=775"]
config.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true
config.vm.network :forwarded_port, guest: 3306, host: 3306, auto_correct: true
config.vm.provision :hostmanager
config.hostmanager.manage_host = true
config.hostmanager.manage_guest = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "1024"
vb.cpus = "1"
vb.name = "Dev"
end
config.vm.define "dev" do |dev|
config.vm.hostname = "dev.local"
dev.vm.network :private_network, ip: '192.168.11.25'
dev.vm.provision "ansible" do |ansible|
ansible.playbook = "provisioning/playbook.yml"
end
end
end
main.yml
#Apache 2 Settings
apache_listen_port: 80
apache_create_vhosts: true
apache_remove_default_vhost: true
apache_global_vhost_settings: |
DirectoryIndex index.php index.html
apache_vhosts:
- servername: "dev.local"
documentroot: "/vagrant/public/"
serveralias: "dev.local"
#PHP Settings
php_use_managed_ini: false
php_error_reporting: "E_ALL & ~E_DEPRECATED & ~E_STRICT"
php_display_errors: "On"
php_display_startup_errors: "On"
php_packages:
- libapache2-mod-php7.0
- php7.0-mysql
- php7.0-curl
- php7.0-mcrypt
- php7.0-mbstring
- php7.0-xml
- php7.0-gd
- php7.0-common
- php7.0-cli
- php7.0-dev
- php7.0-fpm
- libpcre3-dev
- php7.0-imap
- php7.0-json
- php7.0-opcache
- php7.0-intl
- php7.0-zip
- php-sqlite3
- php-apcu
#MySQL Settings
mysql_root_password: root
mysql_root_password_update: yes
mysql_bind_address: '0.0.0.0'
# NodeJS
nodejs_version: "4.x"
#Redis Settings
redis_port: 6379
redis_bind_interface: 0.0.0.0
playbook.yml
---
- name: Dev machine setup
hosts: dev
user: vagrant
become: yes
vars_files:
- vars/main.yml
pre_tasks:
- apt: name=unzip update_cache=yes state=present
roles:
- role: geerlingguy.mysql
- role: geerlingguy.apache
- role: geerlingguy.php
- role: geerlingguy.php-mysql
- role: geerlingguy.nodejs
- role: geerlingguy.redis
- role: geerlingguy.apache-php-fpm
- role: geerlingguy.php-xdebug
- role: geerlingguy.composer
tasks:
- name: Allow root remote access
shell: >
mysql -u root -proot -NBe
"GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'root';"
- name: Ensure MySQL databases are present.
mysql_db:
name: appdb
encoding: utf8
collation: utf8_unicode_ci
# .env File Settings
- name: Copy and rename .env.example
template: src=../../laravel/.env.example dest=/vagrant/.env
- name: set DB_HOST=dev.local
lineinfile: dest=/vagrant/.env regexp='^DB_HOST=' line=DB_HOST=dev.local
- name: set DB_DATABASE=appdb
lineinfile: dest=/vagrant/.env regexp='^DB_DATABASE=' line=DB_DATABASE=appdb
- name: set DB_USERNAME=root
lineinfile: dest=/vagrant/.env regexp='^DB_USERNAME=' line=DB_USERNAME=root
- name: set DB_PASSWORD=root
lineinfile: dest=/vagrant/.env regexp='^DB_PASSWORD=' line=DB_PASSWORD=root
# Storage Settings
- file: path=/vagrant/storage/framework/sessions state=directory mode=0777
- file: path=/vagrant/storage/framework/views state=directory mode=0777
- file: path=/vagrant/storage/framework/cache state=directory mode=0777
# Composer
- composer: command=install working_dir=/vagrant/ no_dev=no
# NPM Global
- name: Install Gulp-cli globally.
npm: name=gulp-cli global=yes
- name: Install gulp globaly
npm: name=gulp global=yes
- name: Install bower globally.
npm: name=bower global=yes
# Artisan Tasks
- name: Install Migrations and Seed
command: php artisan migrate:refresh --seed
become: true
become_user: vagrant
args:
chdir: /vagrant/
- name: Generate a new app key
command: php artisan key:generate
become: true
become_user: vagrant
args:
chdir: /vagrant/
- name: Generate JS Routes
command: php artisan laroute:generate
become: true
become_user: vagrant
args:
chdir: /vagrant/
- name: Create Symbolic Link
file: src=/vagrant/storage/app/public dest=/vagrant/public/storage state=link
It turns out that it was a problem during folder syncing. I've changed to this
config.vm.synced_folder "../laravel", "/vagrant",
:nfs => true,
:linux__nfs_options => ['rw','no_subtree_check','all_squash','async']
And it started working.
Finally!

Resources