使用ansible im尝试 1. sudo进入用户id - hduser 2.设置一些env变量 3.获取文件。
稍后,我打印env变量,但没有看到为用户hduser设置的env变量
- name: change to hduser
shell: sudo su - hduser
- name: Setting
lineinfile:
dest: /home/hduser/.bashrc
line: 'export HADOOP_HOME=/usr/local/hadoop'
insertafter: 'EOF'
regexp: 'export HADOOP_HOME=/usr/local/hadoop'
state: present
- name: Adding the path in the bashrc files
lineinfile:
dest: /home/hduser/.bashrc
line: 'export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64'
insertafter: 'EOF'
regexp: 'export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64'
state: present
- name: Adding the path in the bashrc files
lineinfile:
dest: /home/hduser/.bashrc
line: 'export PATH=$PATH:$HADOOP_HOME/bin'
insertafter: 'EOF'
regexp: 'export PATH=\$PATH:$HADOOP_HOME/bin'
state: present
- name: Source the bashrc file
shell: source /home/hduser/.bashrc
register: sourcevar
args:
executable: /bin/bash
- debug:
var: sourcevar
- name: print env variables
shell: sudo su hduser && env
register: envvar
args:
executable: /bin/bash
- debug:
var: envvar
这些是我的env变量,它没有列出我提供的变量。
ok: [default] => {
"envvar": {
"changed": true,
"cmd": "sudo su hduser && env",
"delta": "0:00:00.013310",
"end": "2017-09-15 17:37:01.913357",
"rc": 0,
"start": "2017-09-15 17:37:01.900047",
"stderr": "",
"stderr_lines": [],
"stdout": "TERM=xterm-256color\nSHELL=/bin/bash\nUSER=root\nSUDO_USER=vagrant\nSUDO_UID=1000\nUSERNAME=root\nMAIL=/var/mail/root\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\nPWD=/home/vagrant\nLANG=en_US.UTF-8\nSHLVL=1\nHOME=/root\nSUDO_COMMAND=/bin/sh -c echo BECOME-SUCCESS-nzuyqifbpqxknsapqpiflhxbmsrzdaqo; /usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1505497021.78-205792997462616/command.py; rm -rf \"/home/vagrant/.ansible/tmp/ansible-tmp-1505497021.78-205792997462616/\" > /dev/null 2>&1\nLOGNAME=root\nSUDO_GID=1000\n_=/usr/bin/env",
"stdout_lines": [
"TERM=xterm-256color",
"SHELL=/bin/bash",
"USER=root",
"SUDO_USER=vagrant",
"SUDO_UID=1000",
"USERNAME=root",
"MAIL=/var/mail/root",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"PWD=/home/vagrant",
"LANG=en_US.UTF-8",
"SHLVL=1",
"HOME=/root",
"SUDO_COMMAND=/bin/sh -c echo BECOME-SUCCESS-nzuyqifbpqxknsapqpiflhxbmsrzdaqo; /usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1505497021.78-205792997462616/command.py; rm -rf \"/home/vagrant/.ansible/tmp/ansible-tmp-1505497021.78-205792997462616/\" > /dev/null 2>&1",
"LOGNAME=root",
"SUDO_GID=1000",
"_=/usr/bin/env"
],
"warnings": [
"Consider using 'become', 'become_method', and 'become_user' rather than running sudo"
]
}
}
答案 0 :(得分:0)
Ansible中的每项任务都是单独处理的。
这样,这是两个独立的任务:
- name: Source the bashrc file
shell: source /home/hduser/.bashrc
register: sourcevar
args:
executable: /bin/bash
- name: print env variables
shell: sudo su hduser && env
register: envvar
第二项任务对第一项任务一无所知。
你应该source
并在一次运行中执行命令:
- shell: source /home/hduser/.bashrc; env
args:
executable: /bin/bash