Automation with Ansible(8) - Running command on Remote computer
- Get link
- X
- Other Apps
Running command on remote
I need to run commands from remote and due to the password prompts, there was a challenge on it. To do this, "pexpect" module should be installed on the remote target computer.
Prerequestic
- "pip" program on remote computer
- Need to install pexpect using pip program
In my case, remote computer name is "docker01" and local computer name is "ns01". However, I recommend to install "pexpect" package on both computer
Reference
Install pip and pexpect module
[root@docker01 ansible]# yum install epel-release Installing : epel-release-7-9.noarch 1/1
Installed:
epel-release.noarch 0:7-9 ...
Complete!
[root@docker01 ansible]# yum -y install python-pip Installed: python2-pip.noarch 0:8.1.2-6.el7 Dependency Installed:
python-backports.x86_64 0:1.0-8.el7 python-backports-ssl_match_hostname.noarch 0:3.4.0.2-4.el7 python-setuptools.noarch 0:0.9.8-7.el7
....
Complete!
[root@docker01 ansible]# pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 696kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.2
Uninstalling pip-8.1.2:
Successfully uninstalled pip-8.1.2
Successfully installed pip-10.0.1
[root@docker01 ansible]# pip install pexpect
Collecting pexpect
Downloading https://files.pythonhosted.org/packages/b9/3c/e51a76fe78a877afd9acd95a51e3b7610c0983b19ef5a5dfa11735a619b8/pexpect-4.5.0-py2.py3-none-any.whl (57kB)
100% |████████████████████████████████| 61kB 745kB/s
Collecting ptyprocess>=0.5 (from pexpect)
Downloading https://files.pythonhosted.org/packages/ff/4e/fa4a73ccfefe2b37d7b6898329e7dbcd1ac846ba3a3b26b294a78a3eb997/ptyprocess-0.5.2-py2.py3-none-any.whl
Installing collected packages: ptyprocess, pexpect
Successfully installed pexpect-4.5.0 ptyprocess-0.5.2
Running command from ansible
During SSH process, two questions can be asked; 1. adding to known hosts with (yes/no) question 2. providing password on remote. From the second question, I used only yes as a question. If you enter without quote on the yes string, it will consider as true/false and will be replaced with True. Please make sure to add quote to the word.
---
- hosts: docker-manager-first
remote_user: root
pre_tasks:
- include_tasks: tasks/load-vars.yml
become: yes
become_method: sudo
tasks:
- name: Get service list from Docker Manager node
become: yes
expect:
command: ssh root@{{DockerManagerNode}} "docker service ls"
responses:
"password": "password"
"yes": "yes"
register: dockerSwarmServiceList
- debug:
var: dockerSwarmServiceList
Ansibleplaybook result
[ansible@ns01 ansible]$ ansible-playbook -vvvv -i inventory/test/internal/cicd_list DeployMicroservices-Test.yaml --extra-vars " ansible_sudo_pass=password" --ask-pass
No config file found; using defaults
SSH password:
PLAYBOOK: DeployMicroservices-Test.yaml *************************************************************************************************************************
1 plays in DeployMicroservices-Test.yaml
PLAY [docker-manager-first] *************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************
changed: [docker01] => {
"changed": true,
"cmd": "ssh root@docker01 \"docker service ls\"",
"delta": "0:00:00.630777",
"end": "2018-04-20 11:50:18.596127",
"invocation": {
"module_args": {
"chdir": null,
"command": "ssh root@docker01 \"docker service ls\"",
"creates": null,
"echo": false,
"removes": null,
"responses": {
"password": "Flans7860",
"yes": "yes"
},
"timeout": 30
}
},
"rc": 0,
"start": "2018-04-20 11:50:17.965350",
"stdout": "The authenticity of host 'docker01 (10.200.0.150)' can't be established.\r\nECDSA key fingerprint is SHA256:cESEZr5cIIDf+uqksrYo+G34m5xm3gu6irysqQbLGN0.\r\nECDSA key fingerprint is MD5:01:7c:8e:b8:15:06:ef:c7:06:8f:b9:3b:f8:9d:9e:23.\r\nAre you sure you want to continue connecting (yes/no)? \r\nWarning: Permanently added 'docker01,10.200.0.150' (ECDSA) to the list of known hosts.\r\r\nroot@docker01's password: \r\nID NAME
MODE REPLICAS IMAGE PORTS\r\n422dsl8pj2vo artifactorystack_artifactory replicated 1/1 docker.bintray.io/jfrog/artifactory-oss:latest *:49200->8081/tcp\r\nj5ayxacd8znf
artifactorystack_postgresql replicated 1/1 postgres:latest *:49201->5432/tcp\r\nk4nfcp42fpkr exactcalllogservice_exactcalllogservice replicated 0/1 harbor.svctest.flair.local/test/exactcalllogservice:latest *:49203->8080/tcp",
"stdout_lines": [
"The authenticity of host 'docker01 (10.200.0.150)' can't be established.",
"ECDSA key fingerprint is SHA256:cESEZr5cIIDf+uqksrYo+G34m5xm3gu6irysqQbLGN0.",
"ECDSA key fingerprint is MD5:01:7c:8e:b8:15:06:ef:c7:06:8f:b9:3b:f8:9d:9e:23.",
"Are you sure you want to continue connecting (yes/no)? ",
"Warning: Permanently added 'docker01,10.200.0.150' (ECDSA) to the list of known hosts.",
"",
"root@docker01's password: ",
"ID NAME MODE REPLICAS IMAGE
PORTS",
"422dsl8pj2vo artifactorystack_artifactory replicated 1/1 docker.bintray.io/jfrog/artifactory-oss:latest
*:49200->8081/tcp",
"j5ayxacd8znf artifactorystack_postgresql replicated 1/1 postgres:latest
*:49201->5432/tcp",
"k4nfcp42fpkr exactcalllogservice_exactcalllogservice replicated 0/1 harbor.svctest.flair.local/test/exactcalllogservice:latest *:49203->8080/tcp"
]
}
TASK [debug] ****************************************************************************************************************************************************
task path: /home/ansible/repo/ansible/DeployMicroservices-Test.yaml:18
ok: [docker01] => {
"dockerSwarmServiceList": {
"changed": true,
"cmd": "ssh root@docker01 \"docker service ls\"",
"delta": "0:00:00.630777",
"end": "2018-04-20 11:50:18.596127",
"failed": false,
"rc": 0,
"start": "2018-04-20 11:50:17.965350",
"stdout": "The authenticity of host 'docker01 (10.200.0.150)' can't be established.\r\nECDSA key fingerprint is SHA256:cESEZr5cIIDf+uqksrYo+G34m5xm3gu6irysqQbLGN0.\r\nECDSA key fingerprint is MD5:01:7c:8e:b8:15:06:ef:c7:06:8f:b9:3b:f8:9d:9e:23.\r\nAre you sure you want to continue connecting (yes/no)? \r\nWarning: Permanently added 'docker01,10.200.0.150' (ECDSA) to the list of known hosts.\r\r\nroot@docker01's password: \r\nID NAME
MODE REPLICAS IMAGE PORTS\r\n422dsl8pj2vo artifactorystack_artifactory replicated 1/1 docker.bintray.io/jfrog/artifactory-oss:latest *:49200->8081/tcp\r\nj5ayxacd8znf
artifactorystack_postgresql replicated 1/1 postgres:latest *:49201->5432/tcp\r\nk4nfcp42fpkr exactcalllogservice_exactcalllogservice replicated 0/1 harbor.svctest.flair.local/test/exactcalllogservice:latest *:49203->8080/tcp",
"stdout_lines": [
"The authenticity of host 'docker01 (10.200.0.150)' can't be established.",
"ECDSA key fingerprint is SHA256:cESEZr5cIIDf+uqksrYo+G34m5xm3gu6irysqQbLGN0.",
"ECDSA key fingerprint is MD5:01:7c:8e:b8:15:06:ef:c7:06:8f:b9:3b:f8:9d:9e:23.",
"Are you sure you want to continue connecting (yes/no)? ",
"Warning: Permanently added 'docker01,10.200.0.150' (ECDSA) to the list of known hosts.",
"",
"root@docker01's password: ",
"ID NAME MODE REPLICAS IMAGE
PORTS",
"422dsl8pj2vo artifactorystack_artifactory replicated 1/1 docker.bintray.io/jfrog/artifactory-oss:latest
*:49200->8081/tcp",
"j5ayxacd8znf artifactorystack_postgresql replicated 1/1 postgres:latest
*:49201->5432/tcp",
"k4nfcp42fpkr exactcalllogservice_exactcalllogservice replicated 0/1 harbor.svctest.flair.local/test/exactcalllogservice:latest *:49203->8080/tcp"
]
}
}
META: ran handlers
META: ran handlers
PLAY RECAP ******************************************************************************************************************************************************
docker01 : ok=9 changed=1 unreachable=0 failed=0
[ansible@ns01 ansible]$
Summary
Fact can't be used from another task. Only fact can be used from sub-task under same inventory definition. Due to this reason, we may want to run command to remoet computer directly and this show how to do it. Next tutorial will look at how Vault feature can be used with this to make more secure on password.
- Get link
- X
- Other Apps
Comments
Post a Comment