Ansible: how to get unarchived path? - ansible

I'm writing an Ansible script to setup a zookeeper cluster. After extract zookeeper tar ball:
unarchive: src={{tmp_dir}}/zookeeper.tar.gz dest=/opt/zookeeper copy=no
I get a zookeeper directory that contains version number:
[root#sc-rdops-vm09-dhcp-2-154 conf]# ls /opt/zookeeper
zookeeper-3.4.8
To proceed, I have to know name of the extracted sub-directory. For example, I need to copy a configure to /opt/zookeeper/zookeeper-3.4.8/conf.
How to get zookeeper-3.4.8 with Ansible statement?

Try with this:
➜ ~ cat become.yml
---
- hosts: localhost
user: vagrant
tasks:
- shell: ls /opt/zookeeper
register: path
- debug: var=path.stdout
➜ ~ ansible-playbook -i hosts-slaves become.yml
[WARNING]: provided hosts list is empty, only localhost is available
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [command] *****************************************************************
changed: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"path.stdout": "zookeeper-3.4.8"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
Then you could use {{ path.stdout }} to set the name of the path, in the next tasks.

Alternatively, before you proceed with the next steps you could rename this directory
- name: Rename directory
shell: mv `ls -d -1 zookeeper*` zookeeper
The above shell command substitution (inside backticks) should work.

Related

Is it possible to run this command grep with multiple slashes as shell command?

I'm new to Ansible I'm trying to run this command to verify AIDE is installed by running in Ansible. when I run my playbook I get non-zero return code error. I'm not sure if single quotes or double quotes needed for my playbook to run correctly. Please I need assistance.
- name: "Verify AIDE to cryptographic mechanisms are set to protect the integrity of audit tools"
become: true
shell: egrep '(\/usr\/sbin\/(audit|au|rsys))' /etc/aide.conf
register: AIDE_Status
I tried to add double quotes between parenthesis of the command returned the same error. Moved quotes between egrep and /etc/aide.conf.
The egrep works as expected. For example, given the file for testing
shell> cat /tmp/aide.conf
\usr\sbin\audit
the playbook
shell> cat pb.yml
- hosts: localhost
tasks:
- command: egrep '\usr\sbin\(audit|au|rsys)' /tmp/aide.conf
register: aide_status
- debug:
var: aide_status
gives
shell> ansible-playbook pb.yml
PLAY [localhost] *****************************************************************************
TASK [command] *******************************************************************************
changed: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] =>
aide_status:
changed: true
cmd:
- egrep
- \usr\sbin\(audit|au|rsys)
- /tmp/aide.conf
delta: '0:00:00.003672'
end: '2022-10-26 22:14:13.735346'
failed: false
msg: ''
rc: 0
start: '2022-10-26 22:14:13.731674'
stderr: ''
stderr_lines: []
stdout: \usr\sbin\audit
stdout_lines:
- \usr\sbin\audit
PLAY RECAP ***********************************************************************************
localhost: ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

List running java processes in a linux host via ansible playbook

I want to list the java processes running in the mentioned hosts. However am not getting the desired o/p
Have created a ansible-playbook file to read the shell commands.
name: 'Check the running java processes'
hosts: all
tasks:
- name: 'check running processes'
shell: ps -ef | grep -i java```
output: PLAY [Check the running java processes] ****************************************
TASK [setup] *******************************************************************
ok: [target1]
ok: [target2]
TASK [check running processes] *************************************************
changed: [target1]
changed: [target2]
PLAY RECAP *********************************************************************
target1 : ok=2 changed=1 unreachable=0 failed=0
target2 : ok=2 changed=1 unreachable=0 failed=0
However, it's not listing the processes.
You could see the result with your actual playbook by running ansible in verbose mode.
However, the good way to do this is to register the output of your task and display its content in a debug task (or use it in any other task/loop).
To know how your registered var looks like, you can read the doc about common and shell's specific return values... or you can simply debug the full var in your playbook and have a look at it.
Here is an example just to get the full stdout of the command:
---
- name: Check the running java processes
hosts: all
tasks:
- name: Check running processes
shell: ps -ef | grep -i java
register: ps_cmd
- name: Show captured processes
debug:
var: ps_cmd.stdout

Unable to get complete output of command module in Ansible

I need to store in variable & print the output of "ls -ltr" for a set of files on remote host. This variable with be used by another play in the same yml.
I tried the following however, it just prints the filename and not the complete results of "ls -ltr" command.
My lstest.yml looks like below:
- name: Play 4- Configure nodes
hosts: remotehost
tasks:
- name: "Collecting APP file information"
command: ls -ltr /app/{{ vars[item.split('.')[1]] }}/{{ item | basename }}
register: fdetails_APP
when: Layer == 'APP'
with_fileglob:
- "{{ playbook_dir }}/tmpfiles/*"
- debug: var=fdetails_APP.stdout_lines
- set_fact: fdet_APP={{ fdetails_APP.stdout_lines }}
- name: Printing fpath
debug:
var: fdet_APP
Output:
TASK [Collecting APP file information]
************************************************************************************ changed: [localhost] =>
(item=/app/Ansible/playbook/tmpfiles/filename1.exe) changed:
[localhost] => (item=/app/Ansible/playbook/tmpfiles/33211.sql)
changed: [localhost] =>
(item=/app/Ansible/playbook/tmpfiles/file1.mrt) changed: [localhost]
=> (item=/app/Ansible/playbook/tmpfiles/filename1.src)
PLAY RECAP
************************************************************************************************************************************************** localhost : ok=3 changed=1 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0
Can you please suggest.
Note. In near future I would also like to add the checksum of the files stored in the same variable.
As per fileglob documentation- "Matching is against local system files on the Ansible controller. To iterate a list of files on a remote node, use the find module." For remote host fileglob cannot be used.

Ansible file creation fails without any error

After running the the below Ansible Yaml file the output shows file is created and the content is changed
The YAML File
---
- hosts: all
gather_facts: yes
connection: local
tasks:
- name: Check the date on the server.
action: command touch /opt/b
- name: cat the Content
action: command cat /opt/b
Running the Playbook
root#my-ubuntu:/var/lib/awx/projects/test# ansible-playbook main.yml
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [ansible-ubuntu-1604-db]
TASK [Check the date on the server.] *******************************************
changed: [ansible-ubuntu-1604-db]
[WARNING]: Consider using file module with state=touch rather than running touch
TASK [cat the Content] *********************************************************
changed: [ansible-ubuntu-1604-db]
PLAY RECAP *********************************************************************
ansible-ubuntu-1604-db : ok=3 changed=2 unreachable=0 failed=0
The Message Display changed=2 and tasks doesnt created any file
ubuntu#ansible-ubuntu-1604-db:~$ ls -l /opt/
total 0
The Env
Ansible Controller on the MAC Local Desktop
Taget Node is on Cloud
With connection: local in your playbook, you tell Ansible to execute all tasks on your local ansible controller. So file is created on your local machine.
Remove connection: local and try again.

ansible \ any reason why .yml role element don't run roles/x/tasks/main.yml

I'm doing exactly like all the tutorials, don't have typos, and even able to run alone the main.yml inside /roles/x
but when I run the play that should call it - nothing really happens
parent
---
- name: Install / Upgrade tagger
hosts: tagger
roles:
- tagger
/roles/tagger/tasks/main.yml
---
- command: echo 1
need to say I'm running everything in localhost.
tried also
ansible-playbook -i "localhost" -c local tagger.yml
ansible-playbook -i "localhost" -c local tagger.yml
[WARNING]: Host file not found: localhost
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [build tagger docker] *****************************************************
TASK [setup] *******************************************************************
ok: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
Using the commandline you gave:
$ ansible-playbook -i "localhost" -c local tagger.yml
ERROR: Unable to find an inventory file, specify one with -i ?
With the obvious correction (adding a comma):
$ ansible-playbook -i "localhost," -c local tagger.yml
PLAY [Install / Upgrade tagger] ***********************************************
skipping: no hosts matched
PLAY RECAP ********************************************************************
That still doesn't match your output, but it does indicate the problem. localhost is never tagger. Perhaps you are using a hosts.ini file and not telling us about it? Or a specific version of ansible that is different than mine? In any case, I changed hosts: tagger to hosts: all as follows:
---
- name: Install / Upgrade tagger
hosts: all
roles:
- tagger
I then reran:
$ ansible-playbook -i "localhost," -c local tagger.yml
PLAY [Install / Upgrade tagger] ***********************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [tagger | command echo 1] ***********************************************
changed: [localhost]
PLAY RECAP ********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
So there are the two fixes necessary.

Resources