Ich kann ssh zum entfernten Host machen und einen source /home/username/.bashrc
machen - alles funktioniert gut ..__ Wenn ich jedoch:
- name: source bashrc
Sudo: no
action: command source /home/username/.bashrc
Ich bekomme:
failed: [hostname] => {"cmd": ["source", "/home/username/.bashrc"], "failed": true, "rc": 2}
msg: [Errno 2] No such file or directory
Ich habe keine Ahnung, was ich falsch mache ...
Sie haben zwei Möglichkeiten, source mit ansible zu verwenden. Einer ist mit dem Befehl "Shell:" und/bin/sh (der Standardwert für ansible). "Quelle" heißt "." in/bin/sh. Ihr Befehl wäre also:
- name: source bashrc
Sudo: no
Shell: . /home/username/.bashrc && [the actual command you want run]
Beachten Sie, dass Sie einen Befehl ausführen müssen, nachdem Sie .bashrc b/c aufgerufen haben. Jede ssh-Sitzung ist unterschiedlich. Jeder ansprechbare Befehl wird in einer separaten ssh-Transaktion ausgeführt.
Die zweite Möglichkeit ist, Ansible Shell zur Verwendung von Bash zu zwingen. Anschließend können Sie den Befehl "source" verwenden:
- name: source bashrc
Sudo: no
Shell: source /home/username/.bashrc && [the actual command you want run]
args:
executable: /bin/bash
Abschließend möchte ich anmerken, dass Sie tatsächlich "/ etc/profile" als Quelle verwenden möchten, wenn Sie Ubuntu oder ähnliches verwenden, wodurch ein lokales Login vollständiger simuliert wird.
command
führt also nur ausführbare Dateien aus. source
an sich ist keine ausführbare Datei. (Dies ist ein integrierter Shell-Befehl.) Gibt es einen Grund, warum Sie eine vollständige Umgebungsvariable source
möchten?
Es gibt andere Möglichkeiten, Umgebungsvariablen in Ansible einzuschließen. Zum Beispiel die Direktive environment
:
- name: My Great Playbook
hosts: all
tasks:
- name: Run my command
Sudo: no
action: command <your-command>
environment:
HOME: /home/myhome
Eine andere Möglichkeit ist die Verwendung des Moduls Shell
Ansible:
- name: source bashrc
Sudo: no
action: Shell source /home/username/.bashrc && <your-command>
oder
- name: source bashrc
Sudo: no
Shell: source /home/username/.bashrc && <your-command>
In diesen Fällen wird die Shell-Instanz/-Umgebung beendet, sobald der Schritt Ansible ausgeführt wird.
Ich weiß, dass diese Antwort zu spät kommt, aber ich habe in genügend Code gesehen, dass Sie die Sudo-Option -i
so verwenden können:
- name: source bashrc
Shell: Sudo -iu {{ansible_user_id}} [the actual command you want run]
Wie in der Dokumentation gesagt
The -i (simulate initial login) option runs the Shell specified by the password database entry of the target user as a login Shell. This means that login-specific
resource files such as .profile or .login will be read by the Shell. If a command is specified, it is passed to the Shell for execution via the Shell's -c option.
If no command is specified, an interactive Shell is executed. Sudo attempts to change to that user's home directory before running the Shell. It also initializes
the environment to a minimal set of variables, similar to what is present when a user logs in. The Command environment section below documents in detail how the -i
option affects the environment in which a command is run.
Dieses Problem trat auch auf, als ich versuchte, den virtualenvwrapper auf einem Ubuntu-Server zum Laufen zu bringen. Ich habe Ansible so benutzt:
- name: Make virtual environment
Shell: source /home/username/.bashrc && makevirtualenv virenvname
args:
executable: /bin/bash
aber der Quellbefehl funktionierte nicht.
Schließlich entdeckte ich, dass die .bashrc-Datei oben in der Datei ein paar Zeilen hat, die verhindern, dass source funktioniert, wenn sie von Ansible aufgerufen wird:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Ich habe diese Zeilen in .bashrc auskommentiert und alles funktionierte danach wie erwartet.
Ich habe die aufgelisteten Antworten ausprobiert, aber diese haben bei der Installation von Ruby über rbenv nicht funktioniert. Ich musste die folgenden Zeilen von /root/.bash_profile
beziehen
PATH=$PATH:$HOME/bin:$HOME/.rbenv/bin:$HOME/.rbenv/plugins/Ruby-build/bin
export PATH
eval "$(rbenv init -)"
Zum Schluss habe ich mir das ausgedacht
- Shell: Sudo su - root -c 'rbenv install -v {{ Ruby_version }}'
Man kann dies mit jedem Befehl verwenden.
- Shell: Sudo su - root -c 'your command'
Während meiner 2 Cent umkreiste ich das Problem bei der Beschaffung von ~/.nvm/nvm.sh
in ~/.profile
und verwende dann Sudo -iu
wie in einer anderen Antwort vorgeschlagen.
Versucht am Januar 2018 gegen Ubuntu 16.04.5
- name: Installing Nvm
Shell: >
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
args:
creates: "/home/{{ ansible_user }}/.nvm/nvm.sh"
tags:
- nodejs
- name: Source nvm in ~/.profile
Sudo: yes
Sudo_user: "{{ ansible_user }}"
lineinfile: >
dest=~/.profile
line="source ~/.nvm/nvm.sh"
create=yes
tags:
- nodejs
register: output
- name: Installing node
command: Sudo -iu {{ ansible_user }} nvm install --lts
args:
executable: /bin/bash
tags:
- nodejs
Ich habe als beste Lösung gefunden:
- name: Source .bashrc
Shell: . .bashrc
become: true
Sie können den Benutzer ändern, indem Sie Folgendes hinzufügen (Standard: root):
- name: Source .bashrc
Shell: . .bashrc
become: true
become-user: {your_remote_user}
Mehr Infos hier: Ansible werden
Ich habe alle oben genannten Optionen mit ansible 2.4.1.0 ausprobiert und niemand arbeitet bis zu zwei anderen und hier ist das Detail, um den Fall neu zu erstellen.
$ cat ~/.bash_aliases
alias ta="echo 'this is test for ansible interactive Shell'";
Und das ist der ansible Test:
- name: Check the basic string operations
hosts: 127.0.0.1
connection: local
tasks:
- name: Test Interactive Bash Failure
Shell: ta
ignore_errors: True
- name: Test Interactive Bash Using Source
Shell: source ~/.bash_aliases && ta
args:
executable: /bin/bash
ignore_errors: yes
- name: Test Interactive Bash Using .
Shell: . ~/.bash_aliases && ta
ignore_errors: yes
- name: Test Interactive Bash Using /bin/bash -ci
Shell: /bin/bash -ic 'ta'
register: result
ignore_errors: yes
- debug: msg="{{ result }}"
- name: Test Interactive Bash Using Sudo -ui
Shell: Sudo -ui hearen ta
register: result
ignore_errors: yes
- name: Test Interactive Bash Using ssh -tt localhost /bin/bash -ci
Shell: ssh -tt localhost /bin/bash -ci 'ta'
register: result
ignore_errors: yes
Und das ist das Ergebnis:
$ ansible-playbook testInteractiveBash.yml
[WARNING]: Could not match supplied Host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [Check the basic string operations] ************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************
ok: [127.0.0.1]
TASK [Test Interactive Bash Failure] ****************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "ta", "delta": "0:00:00.001341", "end": "2018-10-31 10:11:39.485897", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.484556", "stderr": "/bin/sh: 1: ta: not found", "stderr_lines": ["/bin/sh: 1: ta: not found"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Test Interactive Bash Using Source] ***********************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "source ~/.bash_aliases && ta", "delta": "0:00:00.002769", "end": "2018-10-31 10:11:39.588352", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.585583", "stderr": "/bin/bash: ta: command not found", "stderr_lines": ["/bin/bash: ta: command not found"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Test Interactive Bash Using .] ****************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ". ~/.bash_aliases && ta", "delta": "0:00:00.001425", "end": "2018-10-31 10:11:39.682609", "failed": true, "msg": "non-zero return code", "rc": 127, "start": "2018-10-31 10:11:39.681184", "stderr": "/bin/sh: 1: ta: not found", "stderr_lines": ["/bin/sh: 1: ta: not found"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Test Interactive Bash Using /bin/bash -ci] ****************************************************************************************************************************************
changed: [127.0.0.1]
TASK [debug] ****************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
"msg": {
"changed": true,
"cmd": "/bin/bash -ic 'ta'",
"delta": "0:00:00.414534",
"end": "2018-10-31 10:11:40.189365",
"failed": false,
"rc": 0,
"start": "2018-10-31 10:11:39.774831",
"stderr": "",
"stderr_lines": [],
"stdout": "this is test for ansible interactive Shell",
"stdout_lines": [
"this is test for ansible interactive Shell"
]
}
}
TASK [Test Interactive Bash Using Sudo -ui] *********************************************************************************************************************************************
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running Sudo
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "Sudo -ui hearen ta", "delta": "0:00:00.007906", "end": "2018-10-31 10:11:40.306128", "failed": true, "msg": "non-zero return code", "rc": 1, "start": "2018-10-31 10:11:40.298222", "stderr": "Sudo: unknown user: i\nsudo: unable to initialize policy plugin", "stderr_lines": ["Sudo: unknown user: i", "Sudo: unable to initialize policy plugin"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Test Interactive Bash Using ssh -tt localhost /bin/bash -ci] **********************************************************************************************************************
[email protected]'s password:
changed: [127.0.0.1]
PLAY RECAP ******************************************************************************************************************************************************************************
127.0.0.1 : ok=8 changed=6 unreachable=0 failed=0
Es wurden zwei Optionen bearbeitet:
Shell: /bin/bash -ic 'ta'
Shell: ssh -tt localhost /bin/bash -ci 'ta'
, aber dieser erfordert lokal ein Kennwort.