Save ansible variable in local file - ansible

I am executing a PS script on a windows host and want to store its stdout in a file on an ansible local machine. I have a playbook like following:
---
- name: Check Antivirus software
hosts: all
become: false
gather_facts: no
tasks:
- name: Get AV details
win_shell: |
echo "script printing data on stdout"
register: result
- name: save response
copy:
content: '{{ result.stdout }}'
dest: '{{ response_file }}'
delegate_to: localhost
From the above playbook, 1st task gets executed without any issues. But 2nd task gives the following error.
TASK [save response] *******************************************************************************************************************************************
fatal: [20.15.102.192 -> localhost]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPSConnectionPool(host='localhost', port=5986): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f4940760208>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
I also tried local_action which is also giving the same error.

Related

Ansible scp to remto server password

---
- hosts: all
roles:
- role: aos-wlan-ansible-role
tasks:
- name: 获取当前时间
vars:
ansible_connection: local
debug:
msg: "{{ansible_date_time.iso8601_basic_short}}"
register: current_time
- name: 备份配置{{current_time.msg}}.tar.gz 到 flash
aos_show_command:
command: "backup config configbackup_{{current_time.msg}}"
register: result_bak
- name: 上传备份配置{{current_time.msg}}.tar.gz 到 scp 服务器 172.16.0.109/aos_lab1/
aos_show_command:
command: "copy flash: configbackup_{{current_time.msg}}.tar.gz scp: 172.16.0.109 root /root/pythonlab/ansible/configbackup_{{current_time.msg}}.tar.gz"
responses:
(.*)Password: "aruba123"
register: result_copy
failed:
TASK [上传备份配置20220121T164319.tar.gz 到 scp 服务器 172.16.0.109/aos_lab1/] *****************
task path: /Users/leo.ma/onlyleo/python/aruba_python/demo_playbook.yml:17
redirecting (type: connection) ansible.builtin.httpapi to ansible.netcommon.httpapi
fatal: [lab1-mm]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (aos_show_command) module: responses. Supported parameters include: command."}

I'm having RpcTimeoutError commit-configuration

I am using juniper module for Ansible, however I'm having the issue below. Do you have any idea ?
fatal: [172.31.30.158]: FAILED! => {"changed": false, "msg": "Failure checking the configuraton: RpcTimeoutError(host: 172.31.30.158, cmd: commit-configuration, timeout: 10)"}
Playbook:
- name: "Configure Device"
hosts: junos
connection: local
timeout: 50
gather_facts: no
tasks:
- name: "Configure op script"
juniper.device.config:
config_mode: "private"
check_commit_wait: 20
load: "set"
src: "test.txt"
register: response
- name: "Print the config changes"
debug:
var: response

Edit excel file locally based on remote host condition ansible playbook

I am trying to update a XLSX file locally using this module : https://github.com/mohameosam/open_excel
I have succeed to update the XLSX file when running a task without "Conditional Variables". But when I try to run it with "When" variable. It shows error below :
TASK [Update predepchk] ****************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (open_excel) module: when Supported parameters include: attributes, backup, cell_style, content, delimiter, dest, directory_mode, follow, force, group, index_by_name, mode, op, owner, read_range, regexp, remote_src, selevel, serole, setype, seuser, sheet_name, src, unsafe_writes, updates_matrix"}
Here is my ansible yml file :
---
- hosts: all
tasks:
- name: Ping all hosts
ping:
- name: Print a message
debug:
msg: 'All set'
- name: Show facts available on the system
ansible.builtin.debug:
var: ansible_facts
- name: PredepCHK
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Update predepchk
open_excel:
src: "/root/Documents/Ansible/XLSX/predepchk.xlsx"
dest: "/root/Documents/Ansible/XLSX/predepchk_updated.xlsx"
sheet_name: "predepchk"
op: "w"
updates_matrix: "{{ [{'cell_row': 21, 'cell_col': 1, 'cell_value': 1}] }}"
when: ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "9"
My question will be:
Can I pass this remote host condition to my local tasks?
As answered by #Zeitounator .
It's because I have my when: conditional statement where was not the same level as "name: / open_excel:"

How to get response of Ansible telnet module

Below are couple of IP addresses and their telnet response (output)
telnet 10.9.9.112 22
Trying 10.9.9.112......
telnet 10.9.9.143 22
Trying 10.9.9.143.....
telnet: connect to address 10.9.9.143: Connection refused.
For the first IP 10.9.9.112 there is no connection and firewall blocks any connection from source to destination. The output simply says Trying .... and stays that way without printing anything else.
For the second IP 10.9.9.143 i get Connection refused immediately in the output and the control back to the prompt.
I wish to grab both scenarios in when condition and perform different activities for both the cases.
I tried to use Ansible's telnet module but I don't know how to grab both the different outputs in the registered variable.
In my case it prints the same message for both the IPs.
Ansible output for first ip:
TASK [debug] *************************************
ok: [localhost] => {
"msg": "HERE:{u'msg': u'Timeout when waiting for 10.9.9.112', u'failed': True, 'changed': False, u'elapsed': 4}"
Ansible Output for second ip:
TASK [debug] *************************************
ok: [localhost] => {
"msg": "HERE:{u'msg': u'Timeout when waiting for 10.9.9.143', u'failed': True, 'changed': False, u'elapsed': 3}"
The only difference I see is the value for elapsed.
Here is my playbook.
---
- name: "Play 1"
hosts: localhost
tasks:
- wait_for:
hosts: "{{ item }}"
port: 22
state: started
delay: 3
timeout: 90
ignore_errors: yes
register: telnetout
loop:
- 10.9.9.112
- 10.9.9.143
- debug:
msg: "HERE: {{ telnetout }}"
telnet module unfortunately does not record Connection Refused message in the output.
We have to use raw module instead like below.
---
- name: "Play 1"
hosts: localhost
tasks:
- raw: "timeout --signal=9 2 telnet {{ item }} 22"
ignore_errors: yes
register: telnetout
loop:
- 10.9.9.112
- 10.9.9.143
- debug:
msg: "HERE: {{ telnetout }}"

Not able to gather facts of ansible host machine

Set up module in ansible gives an error when i tried to set custom facts on host machine using control machine
---
- hosts: test-servers
gather_facts: false
tasks:
- name: deleting Facts directory
file:
path: /etc/ansible/facts.d/
state: absent
- name: Creates a directiory
file:
path: /etc/ansible/facts.d/
recurse: yes
state: directory
- name: Copy custom date facts to host machine
copy:
src: /app/ansible_poc/roles/custom_facts/templates/facts.d/getdate.fact
dest: /etc/ansible/facts.d/getdate.fact
mode: 0755
- name: Copy custom role facts to host machine
copy:
src: /app/ansible_poc/roles/custom_facts/templates/facts.d/getrole.fact
dest: /etc/ansible/facts.d/getrole.fact
mode: 0755
- name: Reloading facts
setup:
- name: Display message
debug:
msg: "{{ ansible_local.getdate.date.date }}"
- name: Display message
debug:
msg: "{{ ansible_local.getrole.role.role }}"
I get following error when i tried to collect facts of ansible host machine. I have set up a file getdate.fact and getrole.fact which has code respectively
#############getdate.fact###############
echo [date]
echo date= `date`
########################################
#############getrole.fact###############
echo [role]
echo role= `whoami`
########################################
and when i tried to run the playbook main.yml then it following error.
[root#ansibletower tasks]# ansible -m setup test-servers
192.168.111.28 | FAILED! => {
"changed": false,
"cmd": "/etc/ansible/facts.d/getdate.fact",
"msg": "[Errno 8] Exec format error",
"rc": 8
}
192.168.111.27 | FAILED! => {
"changed": false,
"cmd": "/etc/ansible/facts.d/getdate.fact",
"msg": "[Errno 8] Exec format error",
"rc": 8
}
If I recall correctly, executables are expected to return JSON:
#!/bin/bash
echo '{ "date" : "'$( date )'" }'
You probably need to add "shebang" line to your fact scripts. I.e., getdate.fact should look like:
#!/bin/sh
echo [date]
echo date=`date`

Resources