1
votes

I am new to Ansible and I have requirement about ansible running log. Could anyone help? Thanks. I am talking about running log, not the playbook log. eg:

 
 2019-02-28 01:39:59,819 p=23627 u=peterqi |  PLAY [create log file] *******************************************************************************************************>
     2019-02-28 01:39:59,830 p=23627 u=peterqi |  TASK [Gathering Facts] *******************************************************************************************************>
     2019-02-28 01:40:01,129 p=23627 u=peterqi |  ok: [localhost]
     2019-02-28 01:40:01,213 p=23627 u=peterqi |  TASK [rename log file] *******************************************************************************************************>
     2019-02-28 01:40:01,570 p=23627 u=peterqi |  changed: [localhost -> localhost]
     2019-02-28 01:40:01,577 p=23627 u=peterqi |  PLAY [cat test2 log] *********************************************************************************************************>
     2019-02-28 01:40:01,583 p=23627 u=peterqi |  TASK [Gathering Facts] *******************************************************************************************************>
     2019-02-28 01:40:01,682 paramiko.transport starting thread (client mode): 0x5517250L
     2019-02-28 01:40:01,683 paramiko.transport Local version/idstring: SSH-2.0-paramiko_2.4.1
     2019-02-28 01:40:01,690 paramiko.transport Remote version/idstring: SSH-2.0-OpenSSH_6.6.1

Customer has some playbooks to run and only they have the right to update the ansible.cfg I also will develop some playbooks but I must use the coutomer's configuration, both ansible.cfg(log_path) and variable ANSIBLE_LOG_PATH.

Now I want to keep the ansible running log in separate log file ( on for all of my developed playbook, I cannot affect customer's playbook) And another limitation is the playbooks could be executed at the same time.

I try a lot but I find it is almost impossiable.

My first try is adding a task at the beginning of every playbook:


- name: create log file
  hosts: localhost
  tasks:
    - name: rename log file
      shell: /bin/bash -l -c "touch  {{ lookup('env','ANSIBLE_LOG_PATH') }};mv {{ lookup('env','ANSIBLE_LOG_PATH') }} {{ lookup('env','ANSIBLE_LOG_PATH') }}-{{ lookup('pipe','date +%Y%m%d%H%M%S') }}"
      delegate_to: localhost
      become: yes
      become_user: "{{ lookup('env', 'USER') }}"

It works well first but if more than one playbooks are running at the same time, only the first log file will have all the logs, otheres are empty.

I am thinking about create a module, but I do not know if a module will be my solution.

Any suggestion? I also cannot use plugin, because plugin will impact all the playbooks, including customer's.

Thanks a lot for any suggestions. Best Regards, Peter Qin

1

1 Answers

0
votes

You can try the below method. This will work if you are using the same terminal or you are the only user using it. If multiple users are going to run the ansible playbook then every user has run/set the below command. Hope this helps.

  1. You can set the environment variable in the ~/.bash_profile (if you are allowed) or set in the terminal for each session. example: "export ANSIBLE_LOG_PATH=`date +%Y%m%d%H%M%S`.log" .

  2. The other option is to set alias for ansible-playbook in the ~/.bash_profile (if you are allowed to modify) or set in the terminal for each session. example: alias ansible-playbook="ANSIBLE_LOG_PATH=\`date +%Y%m%d%H%M%S\`.log ansible-playbook" .